'use client'

import { useState } from 'react'
import { useRouter, useParams } from 'next/navigation'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card'
import { Button } from '@/components/ui/Button'
import { Modal, ConfirmModal } from '@/components/ui/Modal'
import { SuccessTag, WarningTag, ErrorTag, PendingTag } from '@/components/ui/Tag'
import { ReviewSteps, getBrandReviewSteps } from '@/components/ui/ReviewSteps'
import {
  ArrowLeft,
  FileText,
  CheckCircle,
  XCircle,
  AlertTriangle,
  User,
  Building,
  Clock,
  Eye,
  Download,
  Shield,
  MessageSquare
} from 'lucide-react'

// 模拟脚本任务数据
const mockScriptTask = {
  id: 'script-001',
  title: '夏日护肤推广脚本',
  creatorName: '小美护肤',
  agencyName: '星耀传媒',
  projectName: 'XX品牌618推广',
  submittedAt: '2026-02-06 14:30',
  aiScore: 88,
  status: 'brand_reviewing',
  scriptContent: {
    opening: '大家好！今天给大家分享一款超级好用的夏日护肤神器～',
    productIntro: '这款XX品牌的防晒霜，SPF50+，PA++++，真的是夏天出门必备！质地轻薄不油腻，涂上去清清爽爽的。',
    demo: '我先在手背上试一下，大家看，延展性特别好，轻轻一抹就推开了，完全不会搓泥。',
    closing: '夏天防晒真的很重要，姐妹们赶紧冲！链接在小黄车1号链接～',
  },
  agencyReview: {
    reviewer: '张经理',
    result: 'approved',
    comment: '脚本整体符合要求，卖点覆盖完整，建议通过。',
    reviewedAt: '2026-02-06 15:00',
  },
  aiAnalysis: {
    violations: [
      { id: 'v1', type: '违禁词', content: '神器', suggestion: '建议替换为"好物"或"必备品"', severity: 'medium' },
    ],
    complianceChecks: [
      { item: '品牌名称正确', passed: true },
      { item: 'SPF标注准确', passed: true },
      { item: '无绝对化用语', passed: false, note: '"超级好用"建议修改' },
      { item: '引导语规范', passed: true },
    ],
    sellingPoints: [
      { point: 'SPF50+ PA++++', covered: true },
      { point: '轻薄质地', covered: true },
      { point: '不油腻', covered: true },
      { point: '延展性好', covered: true },
    ],
  },
}

function ReviewProgressBar({ taskStatus }: { taskStatus: string }) {
  const steps = getBrandReviewSteps(taskStatus)
  const currentStep = steps.find(s => s.status === 'current')

  return (
    <Card className="mb-6">
      <CardContent className="py-4">
        <div className="flex items-center justify-between mb-3">
          <span className="text-sm font-medium text-text-primary">审核流程</span>
          <span className="text-sm text-accent-indigo font-medium">
            当前：{currentStep?.label || '品牌方终审'}
          </span>
        </div>
        <ReviewSteps steps={steps} />
      </CardContent>
    </Card>
  )
}

export default function BrandScriptReviewPage() {
  const router = useRouter()
  const params = useParams()
  const [showApproveModal, setShowApproveModal] = useState(false)
  const [showRejectModal, setShowRejectModal] = useState(false)
  const [rejectReason, setRejectReason] = useState('')
  const [viewMode, setViewMode] = useState<'simple' | 'preview'>('preview')

  const task = mockScriptTask

  const handleApprove = () => {
    setShowApproveModal(false)
    alert('审核通过！')
    router.push('/brand/review')
  }

  const handleReject = () => {
    if (!rejectReason.trim()) {
      alert('请填写驳回原因')
      return
    }
    setShowRejectModal(false)
    alert('已驳回')
    router.push('/brand/review')
  }

  return (
    <div className="space-y-4">
      {/* 顶部导航 */}
      <div className="flex items-center gap-4">
        <button type="button" onClick={() => router.back()} className="p-2 hover:bg-bg-elevated rounded-full">
          <ArrowLeft size={20} className="text-text-primary" />
        </button>
        <div className="flex-1">
          <h1 className="text-xl font-bold text-text-primary">{task.title}</h1>
          <div className="flex items-center gap-4 mt-1 text-sm text-text-secondary">
            <span className="flex items-center gap-1">
              <User size={14} />
              {task.creatorName}
            </span>
            <span className="flex items-center gap-1">
              <Building size={14} />
              {task.agencyName}
            </span>
            <span className="flex items-center gap-1">
              <Clock size={14} />
              {task.submittedAt}
            </span>
          </div>
        </div>
        <div className="flex items-center gap-2">
          <button
            type="button"
            onClick={() => setViewMode(viewMode === 'simple' ? 'preview' : 'simple')}
            className="flex items-center gap-2 px-3 py-2 text-sm text-text-secondary hover:text-text-primary hover:bg-bg-elevated rounded-lg"
          >
            <Eye size={16} />
            {viewMode === 'simple' ? '展开预览' : '简洁模式'}
          </button>
        </div>
      </div>

      {/* 审核流程进度条 */}
      <ReviewProgressBar taskStatus={task.status} />

      <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {/* 左侧：脚本内容 */}
        <div className="lg:col-span-2 space-y-4">
          {viewMode === 'simple' ? (
            <Card>
              <CardContent className="py-8 text-center">
                <FileText size={48} className="mx-auto text-accent-indigo mb-4" />
                <p className="text-text-primary font-medium">{task.title}</p>
                <p className="text-sm text-text-secondary mt-2">点击"展开预览"查看脚本内容</p>
                <Button variant="secondary" className="mt-4" onClick={() => setViewMode('preview')}>
                  <Eye size={16} />
                  展开预览
                </Button>
              </CardContent>
            </Card>
          ) : (
            <Card>
              <CardHeader>
                <CardTitle className="flex items-center gap-2">
                  <FileText size={18} className="text-accent-indigo" />
                  脚本内容
                </CardTitle>
              </CardHeader>
              <CardContent className="space-y-4">
                <div className="p-4 bg-bg-elevated rounded-lg">
                  <div className="text-xs text-accent-indigo font-medium mb-2">开场白</div>
                  <p className="text-text-primary">{task.scriptContent.opening}</p>
                </div>
                <div className="p-4 bg-bg-elevated rounded-lg">
                  <div className="text-xs text-purple-400 font-medium mb-2">产品介绍</div>
                  <p className="text-text-primary">{task.scriptContent.productIntro}</p>
                </div>
                <div className="p-4 bg-bg-elevated rounded-lg">
                  <div className="text-xs text-orange-400 font-medium mb-2">使用演示</div>
                  <p className="text-text-primary">{task.scriptContent.demo}</p>
                </div>
                <div className="p-4 bg-bg-elevated rounded-lg">
                  <div className="text-xs text-accent-green font-medium mb-2">结尾引导</div>
                  <p className="text-text-primary">{task.scriptContent.closing}</p>
                </div>
              </CardContent>
            </Card>
          )}

          {/* 代理商初审意见 */}
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <MessageSquare size={18} className="text-blue-500" />
                代理商初审意见
              </CardTitle>
            </CardHeader>
            <CardContent>
              <div className="flex items-start gap-4">
                <div className={`p-2 rounded-full ${task.agencyReview.result === 'approved' ? 'bg-accent-green/20' : 'bg-accent-coral/20'}`}>
                  {task.agencyReview.result === 'approved' ? (
                    <CheckCircle size={20} className="text-accent-green" />
                  ) : (
                    <XCircle size={20} className="text-accent-coral" />
                  )}
                </div>
                <div className="flex-1">
                  <div className="flex items-center gap-2 mb-1">
                    <span className="font-medium text-text-primary">{task.agencyReview.reviewer}</span>
                    <SuccessTag>建议通过</SuccessTag>
                  </div>
                  <p className="text-text-secondary text-sm">{task.agencyReview.comment}</p>
                  <p className="text-xs text-text-tertiary mt-2">{task.agencyReview.reviewedAt}</p>
                </div>
              </div>
            </CardContent>
          </Card>
        </div>

        {/* 右侧：AI 分析面板 */}
        <div className="space-y-4">
          {/* AI 评分 */}
          <Card>
            <CardContent className="py-4">
              <div className="flex items-center justify-between">
                <span className="text-sm text-text-secondary">AI 综合评分</span>
                <span className={`text-3xl font-bold ${task.aiScore >= 85 ? 'text-accent-green' : task.aiScore >= 70 ? 'text-yellow-400' : 'text-accent-coral'}`}>
                  {task.aiScore}
                </span>
              </div>
            </CardContent>
          </Card>

          {/* 违规检测 */}
          <Card>
            <CardHeader className="pb-2">
              <CardTitle className="flex items-center gap-2 text-base">
                <AlertTriangle size={16} className="text-orange-500" />
                违规检测 ({task.aiAnalysis.violations.length})
              </CardTitle>
            </CardHeader>
            <CardContent className="space-y-2">
              {task.aiAnalysis.violations.map((v) => (
                <div key={v.id} className="p-3 bg-orange-500/10 rounded-lg border border-orange-500/30">
                  <div className="flex items-center gap-2 mb-1">
                    <WarningTag>{v.type}</WarningTag>
                  </div>
                  <p className="text-sm text-text-primary">「{v.content}」</p>
                  <p className="text-xs text-accent-indigo mt-1">{v.suggestion}</p>
                </div>
              ))}
              {task.aiAnalysis.violations.length === 0 && (
                <p className="text-sm text-text-tertiary text-center py-4">未发现违规内容</p>
              )}
            </CardContent>
          </Card>

          {/* 合规检查 */}
          <Card>
            <CardHeader className="pb-2">
              <CardTitle className="flex items-center gap-2 text-base">
                <Shield size={16} className="text-accent-indigo" />
                合规检查
              </CardTitle>
            </CardHeader>
            <CardContent className="space-y-2">
              {task.aiAnalysis.complianceChecks.map((check, idx) => (
                <div key={idx} className="flex items-start gap-2 p-2 rounded-lg bg-bg-elevated">
                  {check.passed ? (
                    <CheckCircle size={16} className="text-accent-green flex-shrink-0 mt-0.5" />
                  ) : (
                    <XCircle size={16} className="text-accent-coral flex-shrink-0 mt-0.5" />
                  )}
                  <div className="flex-1">
                    <span className="text-sm text-text-primary">{check.item}</span>
                    {check.note && (
                      <p className="text-xs text-text-tertiary mt-0.5">{check.note}</p>
                    )}
                  </div>
                </div>
              ))}
            </CardContent>
          </Card>

          {/* 卖点覆盖 */}
          <Card>
            <CardHeader className="pb-2">
              <CardTitle className="flex items-center gap-2 text-base">
                <CheckCircle size={16} className="text-accent-green" />
                卖点覆盖
              </CardTitle>
            </CardHeader>
            <CardContent className="space-y-2">
              {task.aiAnalysis.sellingPoints.map((sp, idx) => (
                <div key={idx} className="flex items-center gap-2 p-2 rounded-lg bg-bg-elevated">
                  {sp.covered ? (
                    <CheckCircle size={16} className="text-accent-green" />
                  ) : (
                    <XCircle size={16} className="text-accent-coral" />
                  )}
                  <span className="text-sm text-text-primary">{sp.point}</span>
                </div>
              ))}
            </CardContent>
          </Card>
        </div>
      </div>

      {/* 底部决策栏 */}
      <Card className="sticky bottom-4 shadow-lg">
        <CardContent className="py-4">
          <div className="flex items-center justify-between">
            <div className="text-sm text-text-secondary">
              项目：{task.projectName}
            </div>
            <div className="flex gap-3">
              <Button variant="danger" onClick={() => setShowRejectModal(true)}>
                驳回
              </Button>
              <Button variant="success" onClick={() => setShowApproveModal(true)}>
                通过
              </Button>
            </div>
          </div>
        </CardContent>
      </Card>

      {/* 通过确认弹窗 */}
      <ConfirmModal
        isOpen={showApproveModal}
        onClose={() => setShowApproveModal(false)}
        onConfirm={handleApprove}
        title="确认通过"
        message="确定要通过此脚本的审核吗？通过后达人将收到通知，可以开始拍摄视频。"
        confirmText="确认通过"
      />

      {/* 驳回弹窗 */}
      <Modal isOpen={showRejectModal} onClose={() => setShowRejectModal(false)} title="驳回审核">
        <div className="space-y-4">
          <p className="text-text-secondary text-sm">请填写驳回原因，达人将收到通知并根据您的反馈进行修改。</p>
          <div>
            <label className="block text-sm font-medium text-text-primary mb-1">驳回原因</label>
            <textarea
              className="w-full h-24 p-3 border border-border-subtle rounded-lg resize-none bg-bg-elevated text-text-primary focus:outline-none focus:ring-2 focus:ring-accent-indigo"
              placeholder="请详细说明驳回原因..."
              value={rejectReason}
              onChange={(e) => setRejectReason(e.target.value)}
            />
          </div>
          <div className="flex gap-3 justify-end">
            <Button variant="ghost" onClick={() => setShowRejectModal(false)}>取消</Button>
            <Button variant="danger" onClick={handleReject}>确认驳回</Button>
          </div>
        </div>
      </Modal>
    </div>
  )
}
