'use client';

import { useState } from 'react';
import { useContributionRecords } from '../hooks/use-users';
import { formatDecimal, formatPercent } from '@/lib/utils/format';
import { formatDateTime } from '@/lib/utils/date';
import { Card, CardContent } from '@/components/ui/card';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Button } from '@/components/ui/button';
import { Skeleton } from '@/components/ui/skeleton';
import { ChevronLeft, ChevronRight } from 'lucide-react';

const contributionTypeLabels: Record<string, string> = {
  PERSONAL: '个人',
  SYSTEM_OPERATION: '系统运营',
  SYSTEM_PROVINCE: '系统省级',
  SYSTEM_CITY: '系统市级',
  TEAM_LEVEL: '团队层级',
  TEAM_BONUS: '团队奖励',
};

interface ContributionRecordsListProps {
  accountSequence: string;
}

export function ContributionRecordsList({ accountSequence }: ContributionRecordsListProps) {
  const [page, setPage] = useState(1);
  const pageSize = 10;

  const { data, isLoading } = useContributionRecords(accountSequence, { page, pageSize });

  return (
    <Card>
      <CardContent className="p-0">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>来源账户</TableHead>
              <TableHead>类型</TableHead>
              <TableHead>分配类型</TableHead>
              <TableHead className="text-right">比例</TableHead>
              <TableHead className="text-right">算力值</TableHead>
              <TableHead>层级/等级</TableHead>
              <TableHead>时间</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {isLoading ? (
              [...Array(5)].map((_, i) => (
                <TableRow key={i}>
                  {[...Array(7)].map((_, j) => (
                    <TableCell key={j}>
                      <Skeleton className="h-4 w-full" />
                    </TableCell>
                  ))}
                </TableRow>
              ))
            ) : data?.items.length === 0 ? (
              <TableRow>
                <TableCell colSpan={7} className="text-center py-8 text-muted-foreground">
                  暂无记录
                </TableCell>
              </TableRow>
            ) : (
              data?.items.map((record) => (
                <TableRow key={record.id}>
                  <TableCell className="font-mono">{record.sourceAccountSequence}</TableCell>
                  <TableCell>{contributionTypeLabels[record.contributionType] || record.contributionType}</TableCell>
                  <TableCell>{record.distributionType === 'UPSTREAM' ? '上游' : '下游'}</TableCell>
                  <TableCell className="text-right">{formatPercent(record.rate)}</TableCell>
                  <TableCell className="text-right font-mono">{formatDecimal(record.amount, 4)}</TableCell>
                  <TableCell>
                    {record.levelDepth !== null
                      ? `L${record.levelDepth}`
                      : record.bonusTier !== null
                        ? `T${record.bonusTier}`
                        : '-'}
                  </TableCell>
                  <TableCell className="text-sm">{formatDateTime(record.createdAt)}</TableCell>
                </TableRow>
              ))
            )}
          </TableBody>
        </Table>

        {data && data.totalPages > 1 && (
          <div className="flex items-center justify-between p-4 border-t">
            <p className="text-sm text-muted-foreground">
              共 {data.total} 条，第 {page} / {data.totalPages} 页
            </p>
            <div className="flex items-center gap-2">
              <Button variant="outline" size="sm" onClick={() => setPage(page - 1)} disabled={page <= 1}>
                <ChevronLeft className="h-4 w-4" />
              </Button>
              <Button variant="outline" size="sm" onClick={() => setPage(page + 1)} disabled={page >= data.totalPages}>
                <ChevronRight className="h-4 w-4" />
              </Button>
            </div>
          </div>
        )}
      </CardContent>
    </Card>
  );
}
