import { useState } from 'react';
import { Alert, Form, Input, InputNumber, Modal, Select, Space, message } from 'antd';
import { ProCard } from '@ant-design/pro-components';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import type { ColDef } from '@ag-grid-community/core';
import dayjs from 'dayjs';
import PageContainer from '@/components/common/PageContainer';
import SearchForm from '@/components/common/SearchForm';
import DataGrid from '@/components/common/DataGrid';
import PermissionButton from '@/components/common/PermissionButton';
import {
  commissionApi,
  ChargebackWaiverRow,
  ChargebackWaiverSaveReq,
  CommissionSearchParam,
} from '@/api/commission';
import { GRAY, RADIUS, SHADOW } from '@/theme/tokens';

const fmt = (v: unknown) => (v as number)?.toLocaleString() ?? '-';

const STATUS_OPTIONS = [
  { value: 'REQUESTED', label: '신청' },
  { value: 'APPROVED',  label: '승인' },
  { value: 'REJECTED',  label: '반려' },
];

const COLS: ColDef<ChargebackWaiverRow>[] = [
  { field: 'settleMonth',        headerName: '정산월',     width: 110, pinned: 'left' },
  { field: 'agentName',          headerName: '설계사',     width: 130 },
  { field: 'contractNo',         headerName: '증권번호',   width: 140 },
  { field: 'originalChargeback', headerName: '원환수액',   flex: 1, type: 'numericColumn', valueFormatter: (p) => fmt(p.value) },
  { field: 'waiverAmount',       headerName: '감면액',     flex: 1, type: 'numericColumn', valueFormatter: (p) => fmt(p.value) },
  { field: 'reason',             headerName: '사유',       flex: 1 },
  { field: 'approvalStatus',     headerName: '승인상태',   width: 110 },
  { field: 'approvedAt',         headerName: '승인일시',   width: 160 },
];

export default function ChargebackWaiver() {
  const qc = useQueryClient();
  const [searchParams, setSearchParams] = useState<CommissionSearchParam & { approvalStatus?: string }>({
    settleMonth: dayjs().format('YYYYMM'), pageSize: 200,
  });
  const [createOpen, setCreateOpen] = useState(false);
  const [rejectTarget, setRejectTarget] = useState<ChargebackWaiverRow | null>(null);
  const [rejectReason, setRejectReason] = useState('');
  const [form] = Form.useForm<ChargebackWaiverSaveReq>();

  const { data: listData, isLoading, isError } = useQuery({
    queryKey: ['chargebackWaiver', 'list', searchParams],
    queryFn: () => commissionApi.chargebackWaiverList(searchParams),
    retry: false,
  });

  const rows = listData?.list ?? [];

  const createMutation = useMutation({
    mutationFn: (body: ChargebackWaiverSaveReq) => commissionApi.chargebackWaiverCreate(body),
    onSuccess: () => {
      message.success('신청 완료');
      qc.invalidateQueries({ queryKey: ['chargebackWaiver', 'list'] });
      setCreateOpen(false);
      form.resetFields();
    },
  });

  const approveMutation = useMutation({
    mutationFn: (requestId: number) => commissionApi.chargebackWaiverApprove(requestId),
    onSuccess: () => {
      message.success('승인 완료');
      qc.invalidateQueries({ queryKey: ['chargebackWaiver', 'list'] });
    },
  });

  const rejectMutation = useMutation({
    mutationFn: ({ requestId, reason }: { requestId: number; reason: string }) =>
      commissionApi.chargebackWaiverReject(requestId, reason),
    onSuccess: () => {
      message.success('반려 완료');
      qc.invalidateQueries({ queryKey: ['chargebackWaiver', 'list'] });
      setRejectTarget(null);
      setRejectReason('');
    },
  });

  const handleCreate = async () => {
    const values = await form.validateFields();
    createMutation.mutate(values);
  };

  const handleApprove = (row: ChargebackWaiverRow) => Modal.confirm({
    title: '승인',
    content: `${row.agentName} 감면 신청(감면액: ${fmt(row.waiverAmount)})을 승인하시겠습니까?`,
    onOk: () => approveMutation.mutateAsync(row.requestId),
  });

  const openReject = (row: ChargebackWaiverRow) => {
    setRejectTarget(row);
    setRejectReason('');
  };

  const handleReject = () => {
    if (!rejectReason.trim()) { message.warning('반려 사유를 입력하세요'); return; }
    rejectMutation.mutate({ requestId: rejectTarget!.requestId, reason: rejectReason });
  };

  const pinnedRow = rows.length > 0 ? {
    agentName:          '합계',
    originalChargeback: rows.reduce((s, r) => s + (r.originalChargeback ?? 0), 0),
    waiverAmount:       rows.reduce((s, r) => s + (r.waiverAmount ?? 0), 0),
  } as Partial<ChargebackWaiverRow> : undefined;

  const cardStyle = { background: '#fff', borderRadius: RADIUS.lg, border: `1px solid ${GRAY[100]}`, boxShadow: SHADOW.sm };

  return (
    <PageContainer title="환수감면신청" description="환수 감면 신청·승인·반려 워크플로">
      {isError && (
        <Alert type="warning" showIcon message="API 미응답" description="/api/chargeback-waivers 를 확인하세요." style={{ marginBottom: 16 }} />
      )}

      <SearchForm
        conditions={[
          { type: 'text',   name: 'agentName',      label: '설계사명', span: 6 },
          { type: 'month',  name: 'settleMonth',     label: '정산월',   span: 6 },
          { type: 'code', name: 'approvalStatus', label: '승인상태', span: 6, groupCode: 'WAIVER_STATUS' },
        ]}
        onSearch={(v) => setSearchParams({ ...v as typeof searchParams, pageSize: 200 })}
        onReset={() => setSearchParams({ settleMonth: dayjs().format('YYYYMM'), pageSize: 200 })}
      />

      <ProCard style={cardStyle}>
        <div style={{ marginBottom: 12 }}>
          <PermissionButton menuCode="CHARGEBACK_WAIVER" permCode="CREATE" type="primary" onClick={() => setCreateOpen(true)}>
            + 감면 신청
          </PermissionButton>
        </div>
        <DataGrid<ChargebackWaiverRow>
          rows={rows}
          columns={[
            ...COLS,
            {
              headerName: '액션', width: 180, pinned: 'right',
              cellRenderer: (p: { data: ChargebackWaiverRow }) => (
                <Space>
                  <PermissionButton
                    menuCode="CHARGEBACK_WAIVER" permCode="UPDATE" size="small"
                    disabled={p.data.approvalStatus !== 'REQUESTED'}
                    onClick={() => handleApprove(p.data)}
                  >
                    승인
                  </PermissionButton>
                  <PermissionButton
                    menuCode="CHARGEBACK_WAIVER" permCode="UPDATE" size="small" danger
                    disabled={p.data.approvalStatus !== 'REQUESTED'}
                    onClick={() => openReject(p.data)}
                  >
                    반려
                  </PermissionButton>
                </Space>
              ),
            },
          ]}
          loading={isLoading}
          height={520}
          rowKey="requestId"
          pinnedBottomRow={pinnedRow}
        />
      </ProCard>

      {/* 신청 모달 */}
      <Modal
        title="감면 신청"
        open={createOpen}
        onOk={handleCreate}
        onCancel={() => { setCreateOpen(false); form.resetFields(); }}
        okText="신청"
        cancelText="취소"
        confirmLoading={createMutation.isPending}
      >
        <Form form={form} layout="vertical">
          <Form.Item name="contractId" label="계약 ID" rules={[{ required: true }]}>
            <InputNumber style={{ width: '100%' }} min={1} />
          </Form.Item>
          <Form.Item name="agentId" label="설계사 ID" rules={[{ required: true }]}>
            <InputNumber style={{ width: '100%' }} min={1} />
          </Form.Item>
          <Form.Item name="settleMonth" label="정산월(YYYYMM)" rules={[{ required: true }]}>
            <Input placeholder="예: 202601" />
          </Form.Item>
          <Form.Item name="originalChargeback" label="원 환수액" rules={[{ required: true }]}>
            <InputNumber style={{ width: '100%' }} min={0} />
          </Form.Item>
          <Form.Item name="waiverAmount" label="감면 요청액" rules={[{ required: true }]}>
            <InputNumber style={{ width: '100%' }} min={0} />
          </Form.Item>
          <Form.Item name="reason" label="사유">
            <Input.TextArea rows={3} placeholder="감면 사유를 입력하세요" />
          </Form.Item>
        </Form>
      </Modal>

      {/* 반려 사유 모달 */}
      <Modal
        title="반려 사유 입력"
        open={!!rejectTarget}
        onOk={handleReject}
        onCancel={() => { setRejectTarget(null); setRejectReason(''); }}
        okText="반려"
        okButtonProps={{ danger: true }}
        cancelText="취소"
        confirmLoading={rejectMutation.isPending}
      >
        <Input.TextArea
          rows={3}
          placeholder="반려 사유를 입력하세요"
          value={rejectReason}
          onChange={(e) => setRejectReason(e.target.value)}
        />
      </Modal>
    </PageContainer>
  );
}
