/**
 * 세금계산서 API 클라이언트
 */

import { apiClient } from "./client";

// 비용 유형
export type CostType = "purchase" | "installation" | "repair" | "maintenance" | "disposal" | "other";

// 비용 유형 라벨
export const costTypeLabels: Record<CostType, string> = {
  purchase: "구매",
  installation: "설치",
  repair: "수리",
  maintenance: "유지보수",
  disposal: "폐기",
  other: "기타",
};

// 세금계산서 타입
export interface TaxInvoice {
  id: string;
  company_code: string;
  invoice_number: string;
  invoice_type: "sales" | "purchase";
  invoice_status: "draft" | "issued" | "sent" | "cancelled";
  supplier_business_no: string;
  supplier_name: string;
  supplier_ceo_name: string;
  supplier_address: string;
  supplier_business_type: string;
  supplier_business_item: string;
  buyer_business_no: string;
  buyer_name: string;
  buyer_ceo_name: string;
  buyer_address: string;
  buyer_email: string;
  supply_amount: number;
  tax_amount: number;
  total_amount: number;
  invoice_date: string;
  issue_date: string | null;
  remarks: string;
  order_id: string | null;
  customer_id: string | null;
  attachments: TaxInvoiceAttachment[] | null;
  cost_type: CostType | null; // 비용 유형
  created_date: string;
  updated_date: string;
  writer: string;
}

// 첨부파일 타입
export interface TaxInvoiceAttachment {
  id: string;
  file_name: string;
  file_path: string;
  file_size: number;
  file_type: string;
  uploaded_at: string;
  uploaded_by: string;
}

// 세금계산서 품목 타입
export interface TaxInvoiceItem {
  id: string;
  tax_invoice_id: string;
  company_code: string;
  item_seq: number;
  item_date: string;
  item_name: string;
  item_spec: string;
  quantity: number;
  unit_price: number;
  supply_amount: number;
  tax_amount: number;
  remarks: string;
}

// 생성 DTO
export interface CreateTaxInvoiceDto {
  invoice_type: "sales" | "purchase";
  supplier_business_no?: string;
  supplier_name?: string;
  supplier_ceo_name?: string;
  supplier_address?: string;
  supplier_business_type?: string;
  supplier_business_item?: string;
  buyer_business_no?: string;
  buyer_name?: string;
  buyer_ceo_name?: string;
  buyer_address?: string;
  buyer_email?: string;
  supply_amount: number;
  tax_amount: number;
  total_amount: number;
  invoice_date: string;
  remarks?: string;
  order_id?: string;
  customer_id?: string;
  items?: CreateTaxInvoiceItemDto[];
  attachments?: TaxInvoiceAttachment[];
  cost_type?: CostType; // 비용 유형
}

// 품목 생성 DTO
export interface CreateTaxInvoiceItemDto {
  item_date?: string;
  item_name: string;
  item_spec?: string;
  quantity: number;
  unit_price: number;
  supply_amount: number;
  tax_amount: number;
  remarks?: string;
}

// 목록 조회 파라미터
export interface TaxInvoiceListParams {
  page?: number;
  pageSize?: number;
  invoice_type?: "sales" | "purchase";
  invoice_status?: string;
  start_date?: string;
  end_date?: string;
  search?: string;
  buyer_name?: string;
  cost_type?: CostType; // 비용 유형 필터
}

// 목록 응답
export interface TaxInvoiceListResponse {
  success: boolean;
  data: TaxInvoice[];
  pagination: {
    page: number;
    pageSize: number;
    total: number;
    totalPages: number;
  };
}

// 상세 응답
export interface TaxInvoiceDetailResponse {
  success: boolean;
  data: {
    invoice: TaxInvoice;
    items: TaxInvoiceItem[];
  };
}

// 월별 통계 응답
export interface TaxInvoiceMonthlyStatsResponse {
  success: boolean;
  data: {
    sales: { count: number; supply_amount: number; tax_amount: number; total_amount: number };
    purchase: { count: number; supply_amount: number; tax_amount: number; total_amount: number };
  };
  period: { year: number; month: number };
}

/**
 * 세금계산서 목록 조회
 */
export async function getTaxInvoiceList(
  params?: TaxInvoiceListParams
): Promise<TaxInvoiceListResponse> {
  const response = await apiClient.get("/tax-invoice", { params });
  return response.data;
}

/**
 * 세금계산서 상세 조회
 */
export async function getTaxInvoiceById(id: string): Promise<TaxInvoiceDetailResponse> {
  const response = await apiClient.get(`/tax-invoice/${id}`);
  return response.data;
}

/**
 * 세금계산서 생성
 */
export async function createTaxInvoice(
  data: CreateTaxInvoiceDto
): Promise<{ success: boolean; data: TaxInvoice; message: string }> {
  const response = await apiClient.post("/tax-invoice", data);
  return response.data;
}

/**
 * 세금계산서 수정
 */
export async function updateTaxInvoice(
  id: string,
  data: Partial<CreateTaxInvoiceDto>
): Promise<{ success: boolean; data: TaxInvoice; message: string }> {
  const response = await apiClient.put(`/tax-invoice/${id}`, data);
  return response.data;
}

/**
 * 세금계산서 삭제
 */
export async function deleteTaxInvoice(
  id: string
): Promise<{ success: boolean; message: string }> {
  const response = await apiClient.delete(`/tax-invoice/${id}`);
  return response.data;
}

/**
 * 세금계산서 발행
 */
export async function issueTaxInvoice(
  id: string
): Promise<{ success: boolean; data: TaxInvoice; message: string }> {
  const response = await apiClient.post(`/tax-invoice/${id}/issue`);
  return response.data;
}

/**
 * 세금계산서 취소
 */
export async function cancelTaxInvoice(
  id: string,
  reason?: string
): Promise<{ success: boolean; data: TaxInvoice; message: string }> {
  const response = await apiClient.post(`/tax-invoice/${id}/cancel`, { reason });
  return response.data;
}

/**
 * 월별 통계 조회
 */
export async function getTaxInvoiceMonthlyStats(
  year?: number,
  month?: number
): Promise<TaxInvoiceMonthlyStatsResponse> {
  const params: Record<string, number> = {};
  if (year) params.year = year;
  if (month) params.month = month;
  const response = await apiClient.get("/tax-invoice/stats/monthly", { params });
  return response.data;
}

// 비용 유형별 통계 응답
export interface CostTypeStatsResponse {
  success: boolean;
  data: {
    by_cost_type: Array<{
      cost_type: CostType | null;
      count: number;
      supply_amount: number;
      tax_amount: number;
      total_amount: number;
    }>;
    by_month: Array<{
      year_month: string;
      cost_type: CostType | null;
      count: number;
      total_amount: number;
    }>;
    summary: {
      total_count: number;
      total_amount: number;
      purchase_amount: number;
      installation_amount: number;
      repair_amount: number;
      maintenance_amount: number;
      disposal_amount: number;
      other_amount: number;
    };
  };
  period: { year?: number; month?: number };
}

/**
 * 비용 유형별 통계 조회
 */
export async function getCostTypeStats(
  year?: number,
  month?: number
): Promise<CostTypeStatsResponse> {
  const params: Record<string, number> = {};
  if (year) params.year = year;
  if (month) params.month = month;
  const response = await apiClient.get("/tax-invoice/stats/cost-type", { params });
  return response.data;
}

