import { useState } from "react"
import { toast } from "sonner"
import { useQuery } from "@tanstack/react-query"
import { rosterApi, evidenceApi } from '../../lib/api-services'
import { useAuthStore } from "../../store/authStore"
import Card from "../../components/ui/Card"
import Button from "../../components/ui/Button"
import { AlertTriangle, Scale, ShieldCheck } from "lucide-react"

// ========== 仲裁证据链 ==========

export default function EvidenceChain({ employeeId }: { employeeId: string }) {
  const [verifyResult, setVerifyResult] = useState<any>(null)
  const [verifying, setVerifying] = useState(false)

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

  const handleVerify = async () => {
    setVerifying(true)
    try {
      const records = await evidenceApi.byEmployee(employeeId)
      const results: any[] = []
      for (const r of records) {
        const result = await evidenceApi.verify(r.id)
        results.push({ id: r.id, category: r.category, refId: r.refId, ...result })
      }
      setVerifyResult({ total: records.length, valid: results.filter(r => r.valid).length, invalid: results.filter(r => !r.valid).length, details: results })
      toast.success(`验证完成：${results.filter(r => r.valid).length}/${results.length} 条有效`)
    } catch {
      toast.error('验证失败')
    } finally {
      setVerifying(false)
    }
  }

  if (isLoading) return <div className="text-center py-8 text-gray-400">生成证据链中...</div>
  if (!data) return <div className="text-center py-8 text-gray-400">暂无证据链数据，请确保员工已录入合同、薪酬等信息</div>
  if (!data.evidence || data.evidence.length === 0) return (
    <div className="space-y-3">
      <Card>
        <div className="flex items-center justify-between">
          <div>
            <h2 className="text-xs font-medium flex items-center gap-2"><Scale className="w-4 h-4" />仲裁证据链</h2>
            <div className="text-xs text-gray-500 mt-1">
              {data.employee?.name || '未知'} · {data.employee?.department || '未知'} · 入职{data.employee?.hireDate ? data.employee.hireDate.toString().slice(0, 10) : '未知'}
            </div>
          </div>
        </div>
      </Card>
      <Card><div className="text-center py-8 text-gray-400">该员工暂无证据链记录，录入合同、薪酬、考勤等信息后将自动生成</div></Card>
    </div>
  )

  const categoryColor: Record<string, string> = {
    '劳动关系': 'bg-blue-50 text-blue-700 border-blue-200',
    '薪酬发放': 'bg-green-50 text-green-700 border-green-200',
    '考勤记录': 'bg-amber-50 text-amber-700 border-amber-200',
    '违纪处理': 'bg-red-50 text-red-700 border-red-200',
    '培训签收': 'bg-purple-50 text-purple-700 border-purple-200',
    '绩效考核': 'bg-indigo-50 text-indigo-700 border-indigo-200',
    '解聘记录': 'bg-gray-100 text-gray-700 border-gray-300',
  }

  const handleExport = async () => {
    try {
      const token = useAuthStore.getState().accessToken
      const res = await fetch(`/api/v1/roster/${employeeId}/evidence-chain/export`, {
        headers: token ? { Authorization: `Bearer ${token}` } : {},
      })
      if (!res.ok) throw new Error('导出失败')
      const blob = await res.blob()
      const url = URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = url
      a.download = `仲裁证据链_${data.employee.name}_${new Date().toISOString().slice(0, 10)}.xlsx`
      a.click()
      URL.revokeObjectURL(url)
    } catch {
      toast.error('导出失败')
    }
  }

  const riskStyle: Record<string, string> = {
    DANGER: 'bg-red-50 border-red-300 text-red-700',
    HIGH: 'bg-orange-50 border-orange-300 text-orange-700',
    MEDIUM: 'bg-amber-50 border-amber-300 text-amber-700',
  }
  const riskIcon: Record<string, string> = {
    DANGER: '🔴',
    HIGH: '🟠',
    MEDIUM: '🟡',
  }

  return (
    <div className="space-y-3">
      <Card>
        <div className="flex items-center justify-between">
          <div>
            <h2 className="text-xs font-medium flex items-center gap-2"><Scale className="w-4 h-4" />仲裁证据链</h2>
            <div className="text-xs text-gray-500 mt-1">
              {data.employee.name} · {data.employee.department} · 入职{data.employee.hireDate}
            </div>
          </div>
          <div className="flex items-center gap-4">
            <div className="text-xs text-center">
              <div className="text-gray-500">证据总数</div>
              <div className="text-xl font-bold">{data.summary.total}</div>
            </div>
            <div className="text-xs text-center">
              <div className="text-gray-500">已签字</div>
              <div className="text-xl font-bold text-safe">{data.summary.signed}</div>
            </div>
            <div className="text-xs text-center">
              <div className="text-gray-500">未签字</div>
              <div className="text-xl font-bold text-warning">{data.summary.unsigned}</div>
            </div>
            {data.summary.riskCount > 0 && (
              <div className="text-xs text-center">
                <div className="text-gray-500">风险项</div>
                <div className="text-xl font-bold text-danger">{data.summary.riskCount}</div>
              </div>
            )}
            <Button onClick={handleExport}>导出证据链</Button>
            <Button variant="secondary" onClick={handleVerify} disabled={verifying}>
              <ShieldCheck className="w-4 h-4 mr-1" />
              {verifying ? '验证中...' : '验证完整性'}
            </Button>
          </div>
        </div>
      </Card>

      {data.risks && data.risks.length > 0 && (
        <Card>
          <h3 className="text-xs font-medium mb-3 flex items-center gap-2">
            <AlertTriangle className="w-4 h-4 text-danger" />
            风险提醒（{data.risks.length}项）
          </h3>
          <div className="space-y-2">
            {data.risks.map((r: any, i: number) => (
              <div key={i} className={`border rounded-lg p-3 ${riskStyle[r.level] || 'bg-gray-50 border-gray-200 text-gray-600'}`}>
                <div className="flex items-center gap-2">
                  <span>{riskIcon[r.level] || '⚠'}</span>
                  <span className="font-medium text-xs">{r.title}</span>
                  <span className="text-xs opacity-70">{r.category}</span>
                </div>
                <div className="text-xs mt-1 opacity-90">{r.description}</div>
              </div>
            ))}
          </div>
        </Card>
      )}

      {verifyResult && (
        <Card>
          <h3 className="text-xs font-medium mb-3 flex items-center gap-2">
            <ShieldCheck className="w-4 h-4 text-safe" />
            证据链验证结果
          </h3>
          <div className="flex items-center gap-4 mb-3">
            <div className="text-xs text-center">
              <div className="text-gray-500">总记录</div>
              <div className="text-lg font-bold">{verifyResult.total}</div>
            </div>
            <div className="text-xs text-center">
              <div className="text-gray-500">有效</div>
              <div className="text-lg font-bold text-safe">{verifyResult.valid}</div>
            </div>
            {verifyResult.invalid > 0 && (
              <div className="text-xs text-center">
                <div className="text-gray-500">无效</div>
                <div className="text-lg font-bold text-danger">{verifyResult.invalid}</div>
              </div>
            )}
          </div>
          {verifyResult.invalid > 0 && (
            <div className="space-y-1">
              {verifyResult.details.filter((r: any) => !r.valid).map((r: any, i: number) => (
                <div key={i} className="text-xs border rounded p-2 bg-red-50 border-red-200 text-red-700">
                  <span className="font-medium">{r.category}</span>
                  {r.refId && <span className="text-xs opacity-70 ml-2">ID: {r.refId}</span>}
                  <div className="mt-0.5 opacity-90">哈希不匹配：预期 {r.expectedHash?.slice(0, 16)}...，实际 {r.actualHash?.slice(0, 16)}...</div>
                </div>
              ))}
            </div>
          )}
          {verifyResult.invalid === 0 && (
            <div className="text-xs text-safe flex items-center gap-1">
              <ShieldCheck className="w-3.5 h-3.5" /> 所有证据链哈希验证通过，数据完整无篡改
            </div>
          )}
        </Card>
      )}

      <div className="space-y-2">
        {data.evidence.map((e: any, i: number) => (
          <Card key={i} className={e.riskLevel === 'HIGH' ? 'border-orange-300' : ''}>
            <div className="flex items-start gap-3">
              <span className={`px-2 py-0.5 rounded border text-xs shrink-0 ${categoryColor[e.category] || 'bg-gray-50 text-gray-600 border-gray-200'}`}>
                {e.category}
              </span>
              <div className="flex-1">
                <div className="flex items-center gap-2">
                  <span className="font-medium text-xs">{e.title}</span>
                  <span className="text-xs text-gray-400">{e.date}</span>
                  {e.acknowledged === true && <span className="text-xs text-safe">✓ 已签字</span>}
                  {e.acknowledged === false && <span className="text-xs text-warning">⚠ 未签字</span>}
                  {e.riskLevel === 'HIGH' && <span className="text-xs text-danger">⚠ 高风险</span>}
                </div>
                <div className="text-xs text-gray-600 mt-1">{e.description}</div>
              </div>
            </div>
          </Card>
        ))}
      </div>
    </div>
  )
}

export function generateEvidenceText(data: any): string {
  const lines: string[] = []
  lines.push('========================================')
  lines.push('  劳动仲裁证据链')
  lines.push('========================================')
  lines.push('')
  lines.push(`员工姓名：${data.employee.name}`)
  lines.push(`部门：${data.employee.department}`)
  lines.push(`入职日期：${data.employee.hireDate}`)
  lines.push(`状态：${data.employee.status === 'ACTIVE' ? '在职' : '离职'}`)
  lines.push('')
  lines.push(`证据总数：${data.summary.total} 条`)
  lines.push(`已签字：${data.summary.signed} 条`)
  lines.push(`未签字：${data.summary.unsigned} 条`)
  lines.push('')
  lines.push('----------------------------------------')
  lines.push('')

  let currentCategory = ''
  data.evidence.forEach((e: any, i: number) => {
    if (e.category !== currentCategory) {
      currentCategory = e.category
      lines.push(`【${currentCategory}】`)
      lines.push('')
    }
    lines.push(`${i + 1}. ${e.title}（${e.date}）`)
    lines.push(`   ${e.description}`)
    if (e.acknowledged === true) lines.push('   [已签字确认]')
    if (e.acknowledged === false) lines.push('   [未签字]')
    lines.push('')
  })

  lines.push('----------------------------------------')
  lines.push(`导出时间：${new Date().toLocaleString('zh-CN')}`)
  return lines.join('\n')
}
