/** EmployeeProfile 组件 - 员工详情页 */
import { useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { rosterApi } from '../../lib/api-services'
import { DetailTab, TAB_COUNT_KEYS } from './shared'
import EmployeeProfileShell from './EmployeeProfileShell'
import BasicInfo from './BasicInfo'
import ContractInfo from './ContractInfo'
import PayslipSocialInfo from './PayslipSocialInfo'
import DisciplinaryInfo from './DisciplinaryInfo'
import AttendanceOvertimeInfo from './AttendanceOvertimeInfo'
import PerformanceInfo from './PerformanceInfo'
import TerminationInfo from './TerminationInfo'
import ChangeHistoryTab from './ChangeHistoryTab'
import EvidenceChain from './EvidenceChain'
import AttachmentsTab from './AttachmentsTab'

/**
 * 员工详情档案页 — 使用 EmployeeProfileShell 壳组件统一布局
 */
export default function EmployeeProfile({ employeeId, onBack }: { employeeId: string; onBack: () => void }) {
  const [tab, setTab] = useState<DetailTab>('basic')

  const { data: profile, isLoading } = useQuery<any>({
    queryKey: ['roster-profile', employeeId],
    queryFn: async () => {
      return await rosterApi.profile(employeeId)
    },
  })

  const isActive = profile?.status === 'ACTIVE'
  const HIDDEN_FOR_RESIGNED: DetailTab[] = ['attendance', 'performance']

  const getTabCount = (key: DetailTab): number => {
    const dataKey = TAB_COUNT_KEYS[key]
    if (!dataKey || !profile) return 0
    const data = (profile as any)[dataKey]
    if (!Array.isArray(data)) return 0
    if (key === 'payslip') {
      const monthly = (profile as any).monthlyProcessRecords
      return data.length + (Array.isArray(monthly) ? monthly.length : 0)
    }
    if (key === 'attendance') {
      const training = (profile as any).trainingRecords
      return data.length + (Array.isArray(training) ? training.length : 0)
    }
    return data.length
  }

  if (isLoading) return <div className="text-center py-8 text-gray-400">加载中...</div>
  if (!profile) return <div className="text-center py-8 text-gray-400">员工不存在</div>

  return (
    <EmployeeProfileShell
      profile={profile}
      onBack={onBack}
      getTabCount={getTabCount}
      activeTab={tab}
      onTabChange={setTab}
      isActive={isActive}
      hiddenTabs={HIDDEN_FOR_RESIGNED}
    >
      {(activeTab) => (
        <>
          {activeTab === 'basic' && <BasicInfo profile={profile} employeeId={employeeId} attachments={profile.attachments} />}
          {activeTab === 'attachments' && <AttachmentsTab employeeId={employeeId} attachments={profile.attachments} />}
          {activeTab === 'contract' && <ContractInfo employeeId={employeeId} contracts={profile.contracts} hireDate={profile.hireDate} employee={profile} />}
          {activeTab === 'payslip' && <PayslipSocialInfo payslips={profile.payslips} socialInsRecords={profile.socialInsRecords} housingFundRecords={profile.housingFundRecords} monthlyProcessRecords={profile.monthlyProcessRecords} employeeId={employeeId} />}
          {activeTab === 'disciplinary' && <DisciplinaryInfo employeeId={employeeId} records={profile.disciplinaryRecords} />}
          {activeTab === 'attendance' && <AttendanceOvertimeInfo employeeId={employeeId} attendanceRecords={profile.attendanceRecords} overtimeRecords={profile.overtimeRecords} trainingRecords={profile.trainingRecords} />}
          {activeTab === 'performance' && <PerformanceInfo employeeId={employeeId} records={profile.performanceRecords} />}
          {activeTab === 'termination' && <TerminationInfo employeeId={employeeId} profile={profile} records={profile.terminations} />}
          {activeTab === 'evidence' && <EvidenceChain employeeId={employeeId} />}
          {activeTab === 'history' && <ChangeHistoryTab profile={profile} />}
        </>
      )}
    </EmployeeProfileShell>
  )
}
