'use client';

import { useState } from 'react';
import { useMiningRecords } 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';

interface MiningRecordsListProps {
  accountSequence: string;
}

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

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

  return (
    <Card>
      <CardContent className="p-0">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>分配时间</TableHead>
              <TableHead className="text-right">贡献值占比</TableHead>
              <TableHead className="text-right">获得积分股</TableHead>
              <TableHead className="text-right">价格快照</TableHead>
              <TableHead>记录时间</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {isLoading ? (
              [...Array(5)].map((_, i) => (
                <TableRow key={i}>
                  {[...Array(5)].map((_, j) => (
                    <TableCell key={j}>
                      <Skeleton className="h-4 w-full" />
                    </TableCell>
                  ))}
                </TableRow>
              ))
            ) : data?.items.length === 0 ? (
              <TableRow>
                <TableCell colSpan={5} className="text-center py-8 text-muted-foreground">
                  暂无记录
                </TableCell>
              </TableRow>
            ) : (
              data?.items.map((record) => (
                <TableRow key={record.id}>
                  <TableCell className="font-mono">{record.distributionMinute}</TableCell>
                  <TableCell className="text-right">{formatPercent(record.contributionRatio)}</TableCell>
                  <TableCell className="text-right font-mono text-primary">
                    +{formatDecimal(record.shareAmount, 13)}
                  </TableCell>
                  <TableCell className="text-right font-mono">{formatDecimal(record.priceSnapshot, 8)}</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>
  );
}
