import { createClient } from '@/utils/supabase/server'
import EmployeeTable from '@/components/employees/EmployeeTable'

export default async function EmployeesPage() {
  const supabase = await createClient()

  // Fetch employees with their joined user, company, and role data
  const { data: employees } = await supabase
    .from('employees')
    .select(`
      id,
      user_id,
      company_id,
      role_id,
      department,
      title,
      status,
      created_at,
      companies ( id, name ),
      users ( id, first_name, last_name, email ),
      roles ( id, name, description )
    `)
    .order('created_at', { ascending: false })

  // Fetch companies for the modal
  const { data: companies } = await supabase
    .from('companies')
    .select('id, name')
    .order('name')

  // Fetch roles for the modal
  const { data: roles } = await supabase
    .from('roles')
    .select('id, name, description')
    .order('name')

  return (
    <div className="p-4 sm:p-6 lg:p-8 max-w-7xl mx-auto">
      <EmployeeTable 
        initialEmployees={employees || []} 
        companies={companies || []}
        roles={roles || []}
      />
    </div>
  )
}

