// src/features/admin-deal-detail/RejectDialog.tsx
// Lazy-loaded reject dialog for admin deal detail (route code-splitting C3).
// Mounted only when admin clicks the Reject trigger; preloaded on hover/focus.

'use client';

import { useT } from '@/lib/i18n/react';
import { Stack } from '@/components/ui/layout/Stack';
import { Row } from '@/components/ui/layout/Row';
import { Label } from '@/components/ui/primitives/Label';
import { Textarea } from '@/components/ui/primitives/Textarea';
import { Button } from '@/components/ui/primitives/Button';
import {
  Dialog,
  DialogContent,
  DialogTitle,
  DialogDescription,
} from '@/components/ui/overlays/Dialog';
import {
  Select,
  SelectTrigger,
  SelectValue,
  SelectContent,
  SelectItem,
} from '@/components/ui/primitives/Select';

import type { RejectionReason } from './DealDetail';

export interface RejectDialogProps {
  dealTitle: string;
  reason: RejectionReason | '';
  detail: string;
  loading: boolean;
  actionError: string | null;
  onReasonChange: (value: RejectionReason) => void;
  onDetailChange: (value: string) => void;
  onConfirm: () => void;
  onCancel: () => void;
}

/**
 * Reject dialog for admin deal moderation. Self-contained overlay; parent
 * controls visibility via the `rejectOpen` flag and only mounts this lazy
 * subtree when the dialog is actually requested.
 */
export function RejectDialog({
  dealTitle,
  reason,
  detail,
  loading,
  actionError,
  onReasonChange,
  onDetailChange,
  onConfirm,
  onCancel,
}: RejectDialogProps) {
  const tAdmin = useT('admin');
  const tDetail = useT('admin_deal_detail');
  const tCommon = useT('common');

  return (
    <Dialog open onOpenChange={(isOpen) => !isOpen && onCancel()}>
      <DialogContent>
        <DialogTitle>
          {tAdmin('reject')}: {dealTitle}
        </DialogTitle>
        <DialogDescription>{tAdmin('reject_dialog_desc')}</DialogDescription>
        <Stack gap="4">
          <div>
            <Label htmlFor="deal-reject-reason">{tAdmin('reject_reason_label')}</Label>
            <Select value={reason} onValueChange={(v) => onReasonChange(v as RejectionReason)}>
              <SelectTrigger id="deal-reject-reason">
                <SelectValue placeholder={tDetail('reject_reason_placeholder')} />
              </SelectTrigger>
              <SelectContent>
                <SelectItem value="WRONG_PRICE">{tAdmin('reject_reason_wrong_price')}</SelectItem>
                <SelectItem value="MISSING_BAD_IMAGE">
                  {tAdmin('reject_reason_missing_image')}
                </SelectItem>
                <SelectItem value="CATEGORY_MISMATCH">
                  {tAdmin('reject_reason_category_mismatch')}
                </SelectItem>
                <SelectItem value="CONTENT_POLICY">
                  {tAdmin('reject_reason_content_policy')}
                </SelectItem>
                <SelectItem value="UNCLEAR_DESCRIPTION">
                  {tAdmin('reject_reason_unclear_description')}
                </SelectItem>
                <SelectItem value="OTHER">{tAdmin('reject_reason_other')}</SelectItem>
              </SelectContent>
            </Select>
          </div>
          <div>
            <Label htmlFor="deal-reject-detail">{tAdmin('reject_detail_label')}</Label>
            <Textarea
              id="deal-reject-detail"
              rows={3}
              value={detail}
              onChange={(e) => onDetailChange(e.target.value)}
              placeholder={tAdmin('reject_detail_placeholder')}
            />
          </div>
        </Stack>
        {actionError && (
          <p role="alert" className="text-danger-600 mt-3 text-sm">
            {actionError}
          </p>
        )}
        <Row gap="2" className="mt-4" justify="end">
          <Button variant="danger" onClick={onConfirm} disabled={!reason} loading={loading}>
            {tAdmin('reject')}
          </Button>
          <Button variant="ghost" onClick={onCancel}>
            {tCommon('cancel')}
          </Button>
        </Row>
      </DialogContent>
    </Dialog>
  );
}

export default RejectDialog;
