// components/purchase-requests/create-rfq-dialog.tsx
"use client";

import * as React from "react";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { Badge } from "@/components/ui/badge";
import { Alert, AlertDescription } from "@/components/ui/alert";
import {
  FileText,
  Package,
  AlertCircle,
  CheckCircle,
  User,
  Loader2,
  Info,
  X
} from "lucide-react";
import { toast } from "sonner";
import type { PurchaseRequestView } from "@/db/schema";
import { approvePurchaseRequestsAndCreateRfqs } from "../service";
import { useRouter } from "next/navigation";
import { PurchaseGroupCodeSingleSelector, type PurchaseGroupCodeWithUser } from "@/components/common/selectors/purchase-group-code";

interface CreateRfqDialogProps {
  requests: PurchaseRequestView[];
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onSuccess?: () => void;
}

export function CreateRfqDialog({
  requests,
  open,
  onOpenChange,
  onSuccess,
}: CreateRfqDialogProps) {
  const [isLoading, setIsLoading] = React.useState(false);
  const [selectorOpen, setSelectorOpen] = React.useState(false);
  const [selectedPurchaseGroupCode, setSelectedPurchaseGroupCode] = React.useState<PurchaseGroupCodeWithUser | undefined>(undefined);
  const router = useRouter();


  // 유효한 요청만 필터링 (이미 RFQ 생성된 것 제외)
  const validRequests = requests.filter(r => r.status !== "RFQ생성완료");
  const invalidRequests = requests.filter(r => r.status === "RFQ생성완료");

  const handleSelectPurchaseGroupCode = (code: PurchaseGroupCodeWithUser) => {
    setSelectedPurchaseGroupCode(code);
  };

  const handleSubmit = async () => {
    if (validRequests.length === 0) {
      toast.error("RFQ를 생성할 수 있는 구매 요청이 없습니다");
      return;
    }

    try {
      setIsLoading(true);
      
      const requestIds = validRequests.map(r => r.id);
      const results = await approvePurchaseRequestsAndCreateRfqs(
        requestIds,
        selectedPurchaseGroupCode?.user?.id
      ) as Array<{
        success?: boolean;
        skipped?: boolean;
        requestId: number;
        error?: string;
        message?: string;
      }>;

      const successCount = results.filter(r => r.success).length;
      const skipCount = results.filter(r => r.skipped).length;
      
      if (successCount > 0) {
        toast.success(`${successCount}개의 RFQ가 생성되었습니다`);
      }
      
      if (skipCount > 0) {
        toast.info(`${skipCount}개는 이미 RFQ가 생성되어 건너뛰었습니다`);
      }
      
      onOpenChange(false);
      onSuccess?.();
      router.refresh()
    } catch (error) {
      console.error("RFQ 생성 오류:", error);
      toast.error("RFQ 생성 중 오류가 발생했습니다");
    } finally {
      setIsLoading(false);
    }
  };

  const handleClose = () => {
    if (!isLoading) {
      setSelectedPurchaseGroupCode(undefined);
      onOpenChange(false);
    }
  };

  return (
    <Dialog open={open} onOpenChange={handleClose}>
      <DialogContent className="max-w-4xl">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <FileText className="h-5 w-5" />
            RFQ 생성
          </DialogTitle>
          <DialogDescription>
            선택한 구매 요청을 기반으로 RFQ를 생성합니다. 
            {invalidRequests.length > 0 && " 이미 RFQ가 생성된 항목은 제외됩니다."}
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-4">
          {/* 경고 메시지 */}
          {invalidRequests.length > 0 && (
            <Alert>
              <AlertCircle className="h-4 w-4" />
              <AlertDescription>
                {invalidRequests.length}개 항목은 이미 RFQ가 생성되어 제외됩니다.
              </AlertDescription>
            </Alert>
          )}

          {/* 구매 담당자 선택 */}
          <div className="space-y-2">
            <label className="text-sm font-medium">
              구매 담당자 (선택사항)
            </label>
            <div className="flex gap-2">
              <Button
                type="button"
                variant="outline"
                className="flex-1 justify-start h-10"
                onClick={() => setSelectorOpen(true)}
              >
                <span className="flex items-center gap-2">
                  <User className="h-4 w-4" />
                  {selectedPurchaseGroupCode ? (
                    <>
                      [{selectedPurchaseGroupCode.PURCHASE_GROUP_CODE}] {selectedPurchaseGroupCode.DISPLAY_NAME}
                      {selectedPurchaseGroupCode.user && (
                        <span className="text-muted-foreground">
                          ({selectedPurchaseGroupCode.user.name})
                        </span>
                      )}
                    </>
                  ) : (
                    <span className="text-muted-foreground">
                      구매 담당자를 선택하세요 (선택사항)
                    </span>
                  )}
                </span>
              </Button>
              {selectedPurchaseGroupCode && (
                <Button
                  type="button"
                  variant="outline"
                  size="icon"
                  onClick={() => setSelectedPurchaseGroupCode(undefined)}
                >
                  <X className="h-4 w-4" />
                </Button>
              )}
            </div>
            <p className="text-xs text-muted-foreground">
              구매 담당자를 선택하지 않으면 나중에 지정할 수 있습니다
            </p>
          </div>

          {/* 구매그룹코드 선택 다이얼로그 */}
          <PurchaseGroupCodeSingleSelector
            open={selectorOpen}
            onOpenChange={setSelectorOpen}
            selectedCode={selectedPurchaseGroupCode}
            onCodeSelect={handleSelectPurchaseGroupCode}
            title="구매 담당자 선택"
            description="구매 담당자를 선택하세요"
          />

          {/* RFQ 생성 대상 목록 */}
          <div className="space-y-2">
            <label className="text-sm font-medium">
              RFQ 생성 대상 ({validRequests.length}개)
            </label>
            <div className="border rounded-lg max-h-[300px] overflow-y-auto">
              <Table>
                <TableHeader>
                  <TableRow>
                    <TableHead className="w-[140px]">요청번호</TableHead>
                    <TableHead>요청제목</TableHead>
                    <TableHead className="w-[120px]">프로젝트</TableHead>
                    <TableHead className="w-[100px]">패키지</TableHead>
                    <TableHead className="w-[80px] text-center">품목</TableHead>
                    <TableHead className="w-[80px] text-center">첨부</TableHead>
                  </TableRow>
                </TableHeader>
                <TableBody>
                  {validRequests.length === 0 ? (
                    <TableRow>
                      <TableCell colSpan={6} className="text-center text-muted-foreground py-8">
                        RFQ를 생성할 수 있는 구매 요청이 없습니다
                      </TableCell>
                    </TableRow>
                  ) : (
                    validRequests.map((request) => (
                      <TableRow key={request.id}>
                        <TableCell className="font-mono text-sm">
                          {request.requestCode}
                        </TableCell>
                        <TableCell className="max-w-[250px]">
                          <div className="truncate" title={request.requestTitle || undefined}>
                            {request.requestTitle}
                          </div>
                        </TableCell>
                        <TableCell>
                          <div className="truncate" title={request.projectName || undefined}>
                            {request.projectCode}
                          </div>
                        </TableCell>
                        <TableCell>
                          <div className="truncate" title={request.packageName || undefined}>
                            {request.packageNo}
                          </div>
                        </TableCell>
                        <TableCell className="text-center">
                          {(request.itemCount ?? 0) > 0 && (
                            <Badge variant="secondary" className="gap-1">
                              <Package className="h-3 w-3" />
                              {request.itemCount}
                            </Badge>
                          )}
                        </TableCell>
                        <TableCell className="text-center">
                          {(request.attachmentCount ?? 0) > 0 && (
                            <Badge variant="secondary" className="gap-1">
                              <FileText className="h-3 w-3" />
                              {request.attachmentCount}
                            </Badge>
                          )}
                        </TableCell>
                      </TableRow>
                    ))
                  )}
                </TableBody>
              </Table>
            </div>
          </div>

          {/* 안내 메시지 */}
          <Alert>
            <Info className="h-4 w-4" />
            <AlertDescription>
              <ul className="list-disc list-inside space-y-1 text-sm">
                <li>RFQ 생성 시 구매 요청의 첨부파일이 자동으로 이관됩니다</li>
                <li>구매 요청 상태가 &quot;RFQ생성완료&quot;로 변경됩니다</li>
                <li>각 구매 요청별로 개별 RFQ가 생성됩니다</li>
              </ul>
            </AlertDescription>
          </Alert>
        </div>

        <DialogFooter>
          <Button
            variant="outline"
            onClick={handleClose}
            disabled={isLoading}
          >
            취소
          </Button>
          <Button
            onClick={handleSubmit}
            disabled={isLoading || validRequests.length === 0}
          >
            {isLoading ? (
              <>
                <Loader2 className="mr-2 h-4 w-4 animate-spin" />
                RFQ 생성 중...
              </>
            ) : (
              <>
                <CheckCircle className="mr-2 h-4 w-4" />
                RFQ 생성 ({validRequests.length}개)
              </>
            )}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}