'use client';

/**
 * 기성청구관리 - UniversalListPage 마이그레이션
 *
 * 기존 IntegratedListTemplateV2 → UniversalListPage config 기반으로 변환
 * - 클라이언트 사이드 필터링 (검색, 필터, 정렬)
 * - Stats 카드 클릭 필터링 (activeStatTab)
 * - DateRangeSelector with showQuickButtons
 * - filterConfig (multi: 거래처, 현장명 / single: 상태, 정렬)
 * - 삭제 기능 없음 (조회/수정 전용)
 */

import { useState, useMemo } from 'react';
import { useStatsLoader } from '@/hooks/useStatsLoader';
import { FileText, Pencil } from 'lucide-react';
import { useListHandlers } from '@/hooks/useListHandlers';
import { Button } from '@/components/ui/button';
import { TableCell, TableRow } from '@/components/ui/table';
import { Checkbox } from '@/components/ui/checkbox';
import { MobileCard } from '@/components/organisms/MobileCard';
import {
  UniversalListPage,
  type UniversalListConfig,
  type SelectionHandlers,
  type RowClickHandlers,
  type StatCard,
} from '@/components/templates/UniversalListPage';
import type { ProgressBilling, ProgressBillingStats } from './types';
import {
  PROGRESS_BILLING_STATUS_OPTIONS,
  PROGRESS_BILLING_SORT_OPTIONS,
  PROGRESS_BILLING_STATUS_STYLES,
  PROGRESS_BILLING_STATUS_LABELS,
  MOCK_PARTNERS,
  MOCK_SITES,
} from './types';
import {
  getProgressBillingList,
  getProgressBillingStats,
} from './actions';
import { useDateRange } from '@/hooks';

// 테이블 컬럼 정의
const tableColumns = [
  { key: 'no', label: '번호', className: 'w-[50px] text-center' },
  { key: 'billingNumber', label: '기성청구번호', className: 'w-[140px]', copyable: true },
  { key: 'partnerName', label: '거래처', className: 'w-[120px]', copyable: true },
  { key: 'siteName', label: '현장명', className: 'min-w-[150px]', copyable: true },
  { key: 'round', label: '회차', className: 'w-[60px] text-center', copyable: true },
  { key: 'billingYearMonth', label: '기성청구연월', className: 'w-[110px] text-center', copyable: true },
  { key: 'previousBilling', label: '전회기성', className: 'w-[120px] text-right', copyable: true },
  { key: 'currentBilling', label: '금회기성', className: 'w-[120px] text-right', copyable: true },
  { key: 'cumulativeBilling', label: '누계기성', className: 'w-[120px] text-right', copyable: true },
  { key: 'status', label: '상태', className: 'w-[100px] text-center' },
  { key: 'actions', label: '작업', className: 'w-[80px] text-center' },
];

// 금액 포맷
function formatCurrency(amount: number): string {
  return new Intl.NumberFormat('ko-KR').format(amount);
}

interface ProgressBillingManagementListClientProps {
  initialData?: ProgressBilling[];
  initialStats?: ProgressBillingStats;
}

export default function ProgressBillingManagementListClient({
  initialData = [],
  initialStats,
}: ProgressBillingManagementListClientProps) {
  // ===== 공통 핸들러 Hook =====
  const { handleRowClick, handleEdit } = useListHandlers<ProgressBilling>(
    'construction/billing/progress-billing-management'
  );

  // ===== 외부 상태 (UniversalListPage 외부에서 관리) =====
  const [activeStatTab, setActiveStatTab] = useState<'all' | 'contractWaiting' | 'contractComplete'>('all');
  const { startDate, endDate, setStartDate, setEndDate } = useDateRange('today');
  const { data: stats } = useStatsLoader(getProgressBillingStats, initialStats);
  const [searchQuery, setSearchQuery] = useState('');

  // ===== UniversalListPage Config =====
  const config: UniversalListConfig<ProgressBilling> = useMemo(
    () => ({
      // 페이지 기본 정보
      title: '기성청구관리',
      description: '기성청구를 등록하고 관리합니다.',
      icon: FileText,
      basePath: '/construction/billing/progress-billing-management',

      // ID 추출
      idField: 'id',

      // API 액션
      actions: {
        getList: async () => {
          const result = await getProgressBillingList({
            size: 1000,
            startDate: startDate || undefined,
            endDate: endDate || undefined,
          });
          if (result.success && result.data) {
            return {
              success: true,
              data: result.data.items,
              totalCount: result.data.total,
            };
          }
          return { success: false, error: result.error };
        },
      },

      // 테이블 컬럼
      columns: tableColumns,

      // 클라이언트 사이드 필터링
      clientSideFiltering: true,
      itemsPerPage: 20,

      // 검색 필터
      searchPlaceholder: '기성청구번호, 거래처, 현장명 검색',
      searchFilter: (item, searchValue) => {
        const search = searchValue.toLowerCase();
        return (
          item.billingNumber.toLowerCase().includes(search) ||
          item.partnerName.toLowerCase().includes(search) ||
          item.siteName.toLowerCase().includes(search)
        );
      },

      // 필터 설정
      filterConfig: [
        {
          key: 'partners',
          label: '거래처',
          type: 'multi',
          options: MOCK_PARTNERS,
        },
        {
          key: 'sites',
          label: '현장명',
          type: 'multi',
          options: MOCK_SITES,
        },
        {
          key: 'status',
          label: '상태',
          type: 'single',
          options: PROGRESS_BILLING_STATUS_OPTIONS.filter((o) => o.value !== 'all'),
        },
        {
          key: 'sortBy',
          label: '정렬',
          type: 'single',
          options: PROGRESS_BILLING_SORT_OPTIONS,
        },
      ],
      initialFilters: {
        partners: [],
        sites: [],
        status: 'all',
        sortBy: 'latest',
      },
      filterTitle: '기성청구 필터',

      // 커스텀 필터 함수
      customFilterFn: (items, filterValues) => {
        if (!items || items.length === 0) return items;
        return items.filter((item) => {
          // Stats 탭 필터
          if (activeStatTab === 'contractWaiting' &&
              item.status !== 'billing_waiting' &&
              item.status !== 'approval_waiting') return false;
          if (activeStatTab === 'contractComplete' && item.status !== 'billing_complete') return false;

          // 거래처 필터 (다중선택)
          const partnerFilters = filterValues.partners as string[];
          if (partnerFilters?.length > 0 && !partnerFilters.includes(item.partnerId)) return false;

          // 현장 필터 (다중선택)
          const siteFilters = filterValues.sites as string[];
          if (siteFilters?.length > 0 && !siteFilters.includes(item.siteId)) return false;

          // 상태 필터 (단일선택)
          const statusFilter = filterValues.status as string;
          if (statusFilter && statusFilter !== 'all' && item.status !== statusFilter) return false;

          return true;
        });
      },

      // 커스텀 정렬 함수
      customSortFn: (items, filterValues) => {
        const sorted = [...items];
        const sortBy = (filterValues.sortBy as string) || 'latest';

        switch (sortBy) {
          case 'latest':
            sorted.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
            break;
          case 'oldest':
            sorted.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
            break;
          case 'partnerNameAsc':
            sorted.sort((a, b) => a.partnerName.localeCompare(b.partnerName, 'ko'));
            break;
          case 'partnerNameDesc':
            sorted.sort((a, b) => b.partnerName.localeCompare(a.partnerName, 'ko'));
            break;
          case 'siteNameAsc':
            sorted.sort((a, b) => a.siteName.localeCompare(b.siteName, 'ko'));
            break;
          case 'siteNameDesc':
            sorted.sort((a, b) => b.siteName.localeCompare(a.siteName, 'ko'));
            break;
        }
        return sorted;
      },

      // 검색창 (공통 컴포넌트에서 자동 생성)
      hideSearch: true,
      searchValue: searchQuery,
      onSearchChange: setSearchQuery,

      // 공통 헤더 옵션
      dateRangeSelector: {
        enabled: true,
        startDate,
        endDate,
        onStartDateChange: setStartDate,
        onEndDateChange: setEndDate,
        showQuickButtons: true,
      },

      // Stats 카드
      computeStats: (): StatCard[] => [
        {
          label: '전체 계약',
          value: stats?.total ?? 0,
          icon: FileText,
          iconColor: 'text-blue-600',
          onClick: () => setActiveStatTab('all'),
          isActive: activeStatTab === 'all',
        },
        {
          label: '계약대기',
          value: stats?.contractWaiting ?? 0,
          icon: FileText,
          iconColor: 'text-yellow-600',
          onClick: () => setActiveStatTab('contractWaiting'),
          isActive: activeStatTab === 'contractWaiting',
        },
        {
          label: '계약완료',
          value: stats?.contractComplete ?? 0,
          icon: FileText,
          iconColor: 'text-green-600',
          onClick: () => setActiveStatTab('contractComplete'),
          isActive: activeStatTab === 'contractComplete',
        },
      ],

      // 테이블 행 렌더링
      renderTableRow: (
        item: ProgressBilling,
        index: number,
        globalIndex: number,
        handlers: SelectionHandlers & RowClickHandlers<ProgressBilling>
      ) => (
        <TableRow
          key={item.id}
          className="cursor-pointer hover:bg-muted/50"
          onClick={() => handleRowClick(item)}
        >
          <TableCell className="text-center" onClick={(e) => e.stopPropagation()}>
            <Checkbox checked={handlers.isSelected} onCheckedChange={handlers.onToggle} />
          </TableCell>
          <TableCell className="text-center text-muted-foreground">{globalIndex}</TableCell>
          <TableCell>{item.billingNumber}</TableCell>
          <TableCell>{item.partnerName}</TableCell>
          <TableCell>{item.siteName}</TableCell>
          <TableCell className="text-center">{item.round}차</TableCell>
          <TableCell className="text-center">{item.billingYearMonth}</TableCell>
          <TableCell className="text-right">{formatCurrency(item.previousBilling)}</TableCell>
          <TableCell className="text-right">{formatCurrency(item.currentBilling)}</TableCell>
          <TableCell className="text-right">{formatCurrency(item.cumulativeBilling)}</TableCell>
          <TableCell className="text-center">
            <span className={`px-2 py-1 rounded-full text-xs font-medium ${PROGRESS_BILLING_STATUS_STYLES[item.status]}`}>
              {PROGRESS_BILLING_STATUS_LABELS[item.status]}
            </span>
          </TableCell>
          <TableCell className="text-center">
            {handlers.isSelected && (
              <div className="flex items-center justify-center gap-1">
                <Button
                  variant="ghost"
                  size="icon"
                  className="h-8 w-8"
                  onClick={(e) => {
                    e.stopPropagation();
                    handleEdit(item);
                  }}
                >
                  <Pencil className="h-4 w-4" />
                </Button>
              </div>
            )}
          </TableCell>
        </TableRow>
      ),

      // 모바일 카드 렌더링
      renderMobileCard: (
        item: ProgressBilling,
        index: number,
        globalIndex: number,
        handlers: SelectionHandlers & RowClickHandlers<ProgressBilling>
      ) => (
        <MobileCard
          key={item.id}
          title={item.siteName}
          subtitle={item.billingNumber}
          badge={PROGRESS_BILLING_STATUS_LABELS[item.status]}
          badgeVariant="secondary"
          isSelected={handlers.isSelected}
          onToggle={handlers.onToggle}
          onClick={() => handleRowClick(item)}
          details={[
            { label: '거래처', value: item.partnerName },
            { label: '회차', value: `${item.round}차` },
            { label: '금회기성', value: formatCurrency(item.currentBilling) },
          ]}
        />
      ),
    }),
    [startDate, endDate, searchQuery, activeStatTab, stats, handleRowClick, handleEdit]
  );

  return <UniversalListPage config={config} initialData={initialData} onSearchChange={setSearchQuery} />;
}
