import { useState } from 'react';
import Button from '../components/Button';
import Icon from '../components/Icon';
import Table from '../components/Table';
import { PAGE_CONTEXT_LABEL, PageHeader } from './pageListShared';
import { StrategyFilterBar } from './StrategyListPage';

/**
 * BatchAttributionPage — 批测&归因（Copilot 顶栏 Tab · 批测&归因）
 *
 * 抖音社区-社交-私信场景：策略批测任务、样本回放与问题归因列表。
 */

export const BATCH_ATTRIBUTION_MOCK = [
  {
    id: 'batch-1',
    title: '私信服务策略 V8 · 全链路批测',
    subtitle: '回放近 7 天 186 条私信样本，覆盖进线识别、情绪安抚与转人工闸门',
    category: '策略批测',
    updatedAt: '2026-04-21 10:30',
    versions: [
      {
        v: 3,
        title: '情绪升级样本回放',
        status: '需复核',
        metrics: [
          { label: '通过率', value: '91.4%' },
          { label: '样本量', value: '186' },
          { label: '误触发', value: '12' },
          { label: '待复核', value: '8' },
        ],
      },
      {
        v: 2,
        title: '跨轮上下文衔接验证',
        status: '实验中',
        metrics: [
          { label: '通过率', value: '88.7%' },
          { label: '样本量', value: '96' },
          { label: '断档轮次', value: '11' },
          { label: '待复核', value: '5' },
        ],
      },
      {
        v: 1,
        title: '高风险词命中回归',
        status: '已发布',
        metrics: [
          { label: '通过率', value: '95.2%' },
          { label: '样本量', value: '240' },
          { label: '漏拦截', value: '3' },
          { label: '待复核', value: '0' },
        ],
      },
    ],
  },
  {
    id: 'batch-2',
    title: '好友关系异常 · 意图识别批测',
    subtitle: '校验「被拉黑/取关/互关状态」三类好友关系咨询的意图分流准确率',
    category: '意图归因',
    updatedAt: '2026-04-20 18:45',
    versions: [
      {
        v: 2,
        title: '互关状态变更咨询',
        status: '需复核',
        metrics: [
          { label: '准确率', value: '86.3%' },
          { label: '样本量', value: '128' },
          { label: '误分流', value: '17' },
          { label: '待复核', value: '6' },
        ],
      },
      {
        v: 1,
        title: '被拉黑后私信拦截说明',
        status: '已发布',
        metrics: [
          { label: '准确率', value: '93.8%' },
          { label: '样本量', value: '156' },
          { label: '误分流', value: '9' },
          { label: '待复核', value: '2' },
        ],
      },
    ],
  },
  {
    id: 'batch-3',
    title: '社区违规咨询 · 话术合规批测',
    subtitle: '覆盖账号封禁、作品下架、直播中断 3 类违规解释口径的合规性校验',
    category: '合规批测',
    updatedAt: '2026-04-19 14:12',
    versions: [
      {
        v: 2,
        title: '封禁原因解释口径',
        status: '实验中',
        metrics: [
          { label: '合规率', value: '92.1%' },
          { label: '样本量', value: '210' },
          { label: '越界表述', value: '7' },
          { label: '待复核', value: '4' },
        ],
      },
      {
        v: 1,
        title: '申诉入口引导话术',
        status: '已发布',
        metrics: [
          { label: '合规率', value: '96.5%' },
          { label: '样本量', value: '188' },
          { label: '越界表述', value: '2' },
          { label: '待复核', value: '0' },
        ],
      },
    ],
  },
  {
    id: 'batch-4',
    title: '陌生人消息管控 · 边界场景回放',
    subtitle: '验证非互关私信、骚扰举报、频控拦截等边界场景的处置一致性',
    category: '边界回放',
    updatedAt: '2026-04-18 09:20',
    versions: [
      {
        v: 1,
        title: '非互关首条私信引导',
        status: '草稿中',
        metrics: [
          { label: '通过率', value: '84.6%' },
          { label: '样本量', value: '72' },
          { label: '漏拦截', value: '8' },
          { label: '待复核', value: '3' },
        ],
      },
    ],
  },
  {
    id: 'batch-5',
    title: '0419 批测异常样本 · 归因汇总',
    subtitle: '汇总 4/19 批测未通过样本，按意图识别、话术合规、转人工时机三类归因',
    category: '问题归因',
    updatedAt: '2026-04-17 16:05',
    versions: [
      {
        v: 3,
        title: '转人工时机过早归因',
        status: '需复核',
        metrics: [
          { label: '样本数', value: '14' },
          { label: '占比', value: '37%' },
          { label: '主因', value: '情绪词' },
          { label: '建议', value: '调阈值' },
        ],
      },
      {
        v: 2,
        title: '意图识别偏差归因',
        status: '已发布',
        metrics: [
          { label: '样本数', value: '19' },
          { label: '占比', value: '50%' },
          { label: '主因', value: '好友关系' },
          { label: '建议', value: '补训练' },
        ],
      },
      {
        v: 1,
        title: '话术合规风险归因',
        status: '已发布',
        metrics: [
          { label: '样本数', value: '5' },
          { label: '占比', value: '13%' },
          { label: '主因', value: '规则变更' },
          { label: '建议', value: '更新口径' },
        ],
      },
    ],
  },
];

export const BATCH_ATTRIBUTION_FILTERS = [
  {
    label: '批测类型',
    options: ['策略批测', '意图归因', '合规批测', '边界回放', '问题归因'],
  },
  {
    label: '执行状态',
    options: ['需复核', '实验中', '已发布', '草稿中'],
  },
  {
    label: '负责人',
    options: ['林小北', '陈一诺', '周知远', '顾清禾'],
  },
];

export default function BatchAttributionPage({ embedded = false, shellless = false, initialLoading = false } = {}) {
  const [expandedId, setExpandedId] = useState('batch-1');

  const rootClassName = shellless
    ? 'tfds-page-batch-attribution flex min-h-0 min-w-0 w-full flex-1 flex-col overflow-hidden'
    : embedded
      ? 'tfds-page-batch-attribution flex w-full min-w-0 flex-col overflow-hidden'
      : 'tfds-page-batch-attribution flex flex-1 min-h-0 min-w-0 flex-col overflow-hidden';

  return (
    <div className={rootClassName}>
      <div
        className="flex flex-1 min-h-0 min-w-0 flex-col"
        style={{ padding: shellless ? '16px 24px 24px' : '24px', gap: '16px' }}
      >
        <PageHeader
          title="批测&归因"
          contextLabel={shellless ? undefined : PAGE_CONTEXT_LABEL}
          actions={
            <Button variant="outline-black" icon={<Icon name="play-stroked" />}>
              发起批测
            </Button>
          }
        />

        <StrategyFilterBar filters={BATCH_ATTRIBUTION_FILTERS} />

        <Table
          variant="card-form"
          className="min-h-0 flex-1"
          dataSource={BATCH_ATTRIBUTION_MOCK}
          loading={initialLoading}
          rowKey="id"
          expandedRowKeys={expandedId ? [expandedId] : []}
          onExpandedRowKeysChange={(keys) => setExpandedId(keys[keys.length - 1] || null)}
          pagination={{
            current: 1,
            pageSize: 20,
            total: BATCH_ATTRIBUTION_MOCK.length,
            pageSizeOptions: [10, 20],
          }}
        />
      </div>
    </div>
  );
}
