import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { Plus, Search, Edit, Trash2, CreditCard } from 'lucide-react';
import { format } from 'date-fns';
import { zhCN } from 'date-fns/locale';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { toast } from 'sonner';

import { Button } from '@/client/components/ui/button';
import { Input } from '@/client/components/ui/input';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/client/components/ui/card';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/client/components/ui/table';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/client/components/ui/dialog';
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/client/components/ui/form';
import { Badge } from '@/client/components/ui/badge';
import { Skeleton } from '@/client/components/ui/skeleton';
import { DataTablePagination } from '@/client/admin/components/DataTablePagination';

import { userCardClient } from '@/client/api';
import type { InferRequestType, InferResponseType } from 'hono/client';
import { CreateUserCardDto, UpdateUserCardDto } from '@d8d/server/modules/user-cards/user-card.schema';
import { UserSelector } from '@/client/admin/components/UserSelector';
import { AgentSelector } from '@/client/admin/components/AgentSelector';

type CreateRequest = InferRequestType<typeof userCardClient.$post>['json'];
type UpdateRequest = InferRequestType<typeof userCardClient[':id']['$put']>['json'];
type UserCardResponse = InferResponseType<typeof userCardClient.$get, 200>['data'][0];

const createFormSchema = CreateUserCardDto;
const updateFormSchema = UpdateUserCardDto;

export const UserCardsPage = () => {
  const queryClient = useQueryClient();
  const [searchParams, setSearchParams] = useState({ page: 1, limit: 10, search: '' });
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [isCreateForm, setIsCreateForm] = useState(true);
  const [editingCard, setEditingCard] = useState<UserCardResponse | null>(null);
  const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
  const [cardToDelete, setCardToDelete] = useState<number | null>(null);

  // 表单实例
  const createForm = useForm<CreateRequest>({
    resolver: zodResolver(createFormSchema),
    defaultValues: {
      userId: 0,
      cardNo: '',
      password: '',
      balance: 0,
      state: 1,
      isDefault: 2
    }
  });

  const updateForm = useForm<UpdateRequest>({
    resolver: zodResolver(updateFormSchema),
    defaultValues: {}
  });

  // 数据查询
  const { data, isLoading, refetch } = useQuery({
    queryKey: ['userCards', searchParams],
    queryFn: async () => {
      const res = await userCardClient.$get({
        query: {
          page: searchParams.page,
          pageSize: searchParams.limit,
          keyword: searchParams.search
        }
      });
      if (res.status !== 200) throw new Error('获取用户卡列表失败');
      return await res.json();
    }
  });

  // 创建用户卡
  const createMutation = useMutation({
    mutationFn: async (data: CreateRequest) => {
      const res = await userCardClient.$post({ json: data });
      if (res.status !== 201) throw new Error('创建用户卡失败');
      return await res.json();
    },
    onSuccess: () => {
      toast.success('用户卡创建成功');
      setIsModalOpen(false);
      createForm.reset();
      refetch();
    },
    onError: (error) => {
      toast.error(error.message || '创建用户卡失败');
    }
  });

  // 更新用户卡
  const updateMutation = useMutation({
    mutationFn: async ({ id, data }: { id: number; data: UpdateRequest }) => {
      const res = await userCardClient[':id']['$put']({
        param: { id: id.toString() },
        json: data
      });
      if (res.status !== 200) throw new Error('更新用户卡失败');
      return await res.json();
    },
    onSuccess: () => {
      toast.success('用户卡更新成功');
      setIsModalOpen(false);
      updateForm.reset();
      refetch();
    },
    onError: (error) => {
      toast.error(error.message || '更新用户卡失败');
    }
  });

  // 删除用户卡
  const deleteMutation = useMutation({
    mutationFn: async (id: number) => {
      const res = await userCardClient[':id']['$delete']({
        param: { id: id.toString() }
      });
      if (res.status !== 204) throw new Error('删除用户卡失败');
      return res;
    },
    onSuccess: () => {
      toast.success('用户卡删除成功');
      setDeleteDialogOpen(false);
      refetch();
    },
    onError: (error) => {
      toast.error(error.message || '删除用户卡失败');
    }
  });

  // 处理搜索
  const handleSearch = (e: React.FormEvent) => {
    e.preventDefault();
    setSearchParams(prev => ({ ...prev, page: 1 }));
  };

  // 处理创建用户卡
  const handleCreateCard = () => {
    setIsCreateForm(true);
    setEditingCard(null);
    createForm.reset();
    setIsModalOpen(true);
  };

  // 处理编辑用户卡
  const handleEditCard = (card: UserCardResponse) => {
    setIsCreateForm(false);
    setEditingCard(card);
    updateForm.reset({
      userId: card.userId,
      agentId: card.agentId || undefined,
      cardNo: card.cardNo,
      sjtCardNo: card.sjtCardNo || undefined,
      password: card.password,
      authCode: card.authCode || undefined,
      state: card.state,
      balance: card.balance,
      isDefault: card.isDefault
    });
    setIsModalOpen(true);
  };

  // 处理删除用户卡
  const handleDeleteCard = (id: number) => {
    setCardToDelete(id);
    setDeleteDialogOpen(true);
  };

  // 确认删除
  const confirmDelete = () => {
    if (cardToDelete) {
      deleteMutation.mutate(cardToDelete);
    }
  };

  // 加载状态
  if (isLoading) {
    return (
      <div className="space-y-4">
        <div className="flex justify-between items-center">
          <Skeleton className="h-8 w-48" />
          <Skeleton className="h-10 w-32" />
        </div>
        
        <Card>
          <CardContent className="pt-6">
            <div className="space-y-3">
              {[...Array(5)].map((_, i) => (
                <div key={i} className="flex gap-4">
                  <Skeleton className="h-10 flex-1" />
                  <Skeleton className="h-10 flex-1" />
                  <Skeleton className="h-10 flex-1" />
                  <Skeleton className="h-10 w-20" />
                </div>
              ))}
            </div>
          </CardContent>
        </Card>
      </div>
    );
  }

  return (
    <div className="space-y-4">
      {/* 页面标题 */}
      <div className="flex justify-between items-center">
        <div>
          <h1 className="text-2xl font-bold">用户卡管理</h1>
          <p className="text-muted-foreground">管理用户卡和余额信息</p>
        </div>
        <Button onClick={handleCreateCard}>
          <Plus className="mr-2 h-4 w-4" />
          创建用户卡
        </Button>
      </div>

      {/* 搜索区域 */}
      <Card>
        <CardHeader>
          <CardTitle>用户卡列表</CardTitle>
          <CardDescription>查看和管理所有用户卡</CardDescription>
        </CardHeader>
        <CardContent>
          <form onSubmit={handleSearch} className="flex gap-2">
            <div className="relative flex-1 max-w-sm">
              <Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
              <Input
                placeholder="搜索卡号、盛京通卡号..."
                value={searchParams.search}
                onChange={(e) => setSearchParams(prev => ({ ...prev, search: e.target.value }))}
                className="pl-8"
              />
            </div>
            <Button type="submit" variant="outline">
              搜索
            </Button>
          </form>
        </CardContent>
      </Card>

      {/* 数据表格 */}
      <Card>
        <CardContent className="p-0">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>ID</TableHead>
                <TableHead>用户</TableHead>
                <TableHead>卡号</TableHead>
                <TableHead>盛京通卡号</TableHead>
                <TableHead>代理商</TableHead>
                <TableHead>余额</TableHead>
                <TableHead>状态</TableHead>
                <TableHead>默认</TableHead>
                <TableHead>创建时间</TableHead>
                <TableHead className="text-right">操作</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {data?.data.map((card) => (
                <TableRow key={card.id}>
                  <TableCell>{card.id}</TableCell>
                  <TableCell>{card.user?.name || card.user?.username || '-'}</TableCell>
                  <TableCell>{card.cardNo}</TableCell>
                  <TableCell>{card.sjtCardNo || '-'}</TableCell>
                  <TableCell>{card.agent?.name || '-'}</TableCell>
                  <TableCell>¥{card.balance.toFixed(2)}</TableCell>
                  <TableCell>
                    <Badge variant={card.state === 1 ? 'default' : 'secondary'}>
                      {card.state === 1 ? '绑定' : '解绑'}
                    </Badge>
                  </TableCell>
                  <TableCell>
                    <Badge variant={card.isDefault === 1 ? 'default' : 'secondary'}>
                      {card.isDefault === 1 ? '是' : '否'}
                    </Badge>
                  </TableCell>
                  <TableCell>
                    {format(new Date(card.createdAt), 'yyyy-MM-dd HH:mm', { locale: zhCN })}
                  </TableCell>
                  <TableCell className="text-right">
                    <div className="flex justify-end gap-2">
                      <Button
                        variant="ghost"
                        size="icon"
                        onClick={() => handleEditCard(card)}
                      >
                        <Edit className="h-4 w-4" />
                      </Button>
                      <Button
                        variant="ghost"
                        size="icon"
                        onClick={() => handleDeleteCard(card.id)}
                      >
                        <Trash2 className="h-4 w-4" />
                      </Button>
                    </div>
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
          
          {data?.data.length === 0 && (
            <div className="text-center py-8">
              <p className="text-muted-foreground">暂无用户卡数据</p>
            </div>
          )}
        </CardContent>
      </Card>

      {/* 分页 */}
      {data && data.data.length > 0 && (
        <DataTablePagination
          currentPage={searchParams.page}
          pageSize={searchParams.limit}
          totalCount={data.pagination.total || 0}
          onPageChange={(page, limit) => setSearchParams(prev => ({ ...prev, page, limit }))}
        />
      )}

      {/* 创建/编辑模态框 */}
      <Dialog open={isModalOpen} onOpenChange={setIsModalOpen}>
        <DialogContent className="sm:max-w-[500px] max-h-[90vh] overflow-y-auto">
          <DialogHeader>
            <DialogTitle>{isCreateForm ? '创建用户卡' : '编辑用户卡'}</DialogTitle>
            <DialogDescription>
              {isCreateForm ? '创建一个新的用户卡' : '编辑现有用户卡信息'}
            </DialogDescription>
          </DialogHeader>

          {isCreateForm ? (
            <Form {...createForm}>
              <form onSubmit={createForm.handleSubmit((data) => createMutation.mutate(data))} className="space-y-4">
                <FormField
                  control={createForm.control}
                  name="userId"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="flex items-center">
                        用户 <span className="text-red-500 ml-1">*</span>
                      </FormLabel>
                      <FormControl>
                        <UserSelector
                          value={field.value}
                          onChange={field.onChange}
                          placeholder="选择用户"
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="cardNo"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="flex items-center">
                        卡号 <span className="text-red-500 ml-1">*</span>
                      </FormLabel>
                      <FormControl>
                        <Input placeholder="请输入卡号" {...field} />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="password"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="flex items-center">
                        密码 <span className="text-red-500 ml-1">*</span>
                      </FormLabel>
                      <FormControl>
                        <Input type="password" placeholder="请输入密码" {...field} />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="agentId"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>代理商</FormLabel>
                      <FormControl>
                        <AgentSelector
                          value={field.value || undefined}
                          onChange={field.onChange}
                          placeholder="选择代理商"
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="sjtCardNo"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>盛京通卡号</FormLabel>
                      <FormControl>
                        <Input placeholder="请输入盛京通卡号" {...field} />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="balance"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>初始余额</FormLabel>
                      <FormControl>
                        <Input
                          type="number"
                          step="0.01"
                          placeholder="0.00"
                          {...field}
                          onChange={(e) => field.onChange(parseFloat(e.target.value))}
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="state"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>状态</FormLabel>
                      <FormControl>
                        <select
                          {...field}
                          className="w-full px-3 py-2 border rounded-md"
                          onChange={(e) => field.onChange(parseInt(e.target.value))}
                        >
                          <option value={1}>绑定</option>
                          <option value={2}>解绑</option>
                        </select>
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={createForm.control}
                  name="isDefault"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>是否默认</FormLabel>
                      <FormControl>
                        <select
                          {...field}
                          className="w-full px-3 py-2 border rounded-md"
                          onChange={(e) => field.onChange(parseInt(e.target.value))}
                        >
                          <option value={1}>是</option>
                          <option value={2}>否</option>
                        </select>
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <DialogFooter>
                  <Button type="button" variant="outline" onClick={() => setIsModalOpen(false)}>
                    取消
                  </Button>
                  <Button type="submit" disabled={createMutation.isPending}>
                    {createMutation.isPending ? '创建中...' : '创建'}
                  </Button>
                </DialogFooter>
              </form>
            </Form>
          ) : (
            <Form {...updateForm}>
              <form onSubmit={updateForm.handleSubmit((data) => updateMutation.mutate({ id: editingCard!.id, data }))} className="space-y-4">
                <FormField
                  control={updateForm.control}
                  name="userId"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="flex items-center">
                        用户 <span className="text-red-500 ml-1">*</span>
                      </FormLabel>
                      <FormControl>
                        <UserSelector
                          value={field.value || editingCard?.userId || 0}
                          onChange={field.onChange}
                          placeholder="选择用户"
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="cardNo"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="flex items-center">
                        卡号 <span className="text-red-500 ml-1">*</span>
                      </FormLabel>
                      <FormControl>
                        <Input placeholder="请输入卡号" {...field} />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="password"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="flex items-center">
                        密码 <span className="text-red-500 ml-1">*</span>
                      </FormLabel>
                      <FormControl>
                        <Input type="password" placeholder="请输入密码" {...field} />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="agentId"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>代理商</FormLabel>
                      <FormControl>
                        <AgentSelector
                          value={field.value || editingCard?.agentId || undefined}
                          onChange={field.onChange}
                          placeholder="选择代理商"
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="sjtCardNo"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>盛京通卡号</FormLabel>
                      <FormControl>
                        <Input placeholder="请输入盛京通卡号" {...field} />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="balance"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>余额</FormLabel>
                      <FormControl>
                        <Input
                          type="number"
                          step="0.01"
                          placeholder="0.00"
                          {...field}
                          onChange={(e) => field.onChange(parseFloat(e.target.value))}
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="state"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>状态</FormLabel>
                      <FormControl>
                        <select
                          {...field}
                          className="w-full px-3 py-2 border rounded-md"
                          onChange={(e) => field.onChange(parseInt(e.target.value))}
                        >
                          <option value={1}>绑定</option>
                          <option value={2}>解绑</option>
                        </select>
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <FormField
                  control={updateForm.control}
                  name="isDefault"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>是否默认</FormLabel>
                      <FormControl>
                        <select
                          {...field}
                          className="w-full px-3 py-2 border rounded-md"
                          onChange={(e) => field.onChange(parseInt(e.target.value))}
                        >
                          <option value={1}>是</option>
                          <option value={2}>否</option>
                        </select>
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                <DialogFooter>
                  <Button type="button" variant="outline" onClick={() => setIsModalOpen(false)}>
                    取消
                  </Button>
                  <Button type="submit" disabled={updateMutation.isPending}>
                    {updateMutation.isPending ? '更新中...' : '更新'}
                  </Button>
                </DialogFooter>
              </form>
            </Form>
          )}
        </DialogContent>
      </Dialog>

      {/* 删除确认对话框 */}
      <Dialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>确认删除</DialogTitle>
            <DialogDescription>
              确定要删除这张用户卡吗？此操作将同时删除相关的余额记录。
            </DialogDescription>
          </DialogHeader>
          <DialogFooter>
            <Button variant="outline" onClick={() => setDeleteDialogOpen(false)}>
              取消
            </Button>
            <Button variant="destructive" onClick={confirmDelete} disabled={deleteMutation.isPending}>
              {deleteMutation.isPending ? '删除中...' : '删除'}
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  );
};