/**
 * 계정과목 공용 타입 및 상수
 *
 * 모든 회계 모듈에서 공유하는 계정과목 관련 타입/상수 정의.
 * 기존 각 모듈별 ACCOUNT_SUBJECT_OPTIONS, AccountSubjectCategory 등을 대체.
 */

// ===== 계정과목 분류 (대분류) =====
export type AccountSubjectCategory = 'asset' | 'liability' | 'capital' | 'revenue' | 'expense';

export const ACCOUNT_CATEGORY_OPTIONS: { value: AccountSubjectCategory; label: string }[] = [
  { value: 'asset', label: '자산' },
  { value: 'liability', label: '부채' },
  { value: 'capital', label: '자본' },
  { value: 'revenue', label: '수익' },
  { value: 'expense', label: '비용' },
];

export const ACCOUNT_CATEGORY_FILTER_OPTIONS: { value: string; label: string }[] = [
  { value: 'all', label: '전체' },
  ...ACCOUNT_CATEGORY_OPTIONS,
];

export const ACCOUNT_CATEGORY_LABELS: Record<AccountSubjectCategory, string> = {
  asset: '자산',
  liability: '부채',
  capital: '자본',
  revenue: '수익',
  expense: '비용',
};

// ===== 중분류 =====
export type AccountSubCategory =
  | 'current_asset'
  | 'fixed_asset'
  | 'current_liability'
  | 'long_term_liability'
  | 'capital'
  | 'sales_revenue'
  | 'other_revenue'
  | 'cogs'
  | 'selling_admin'
  | 'other_expense';

export const SUB_CATEGORY_LABELS: Record<AccountSubCategory, string> = {
  current_asset: '유동자산',
  fixed_asset: '비유동자산',
  current_liability: '유동부채',
  long_term_liability: '비유동부채',
  capital: '자본',
  sales_revenue: '매출',
  other_revenue: '영업외수익',
  cogs: '매출원가',
  selling_admin: '판매비와관리비',
  other_expense: '영업외비용',
};

// ===== 부문 =====
export type DepartmentType = 'common' | 'manufacturing' | 'admin';

export const DEPARTMENT_TYPE_LABELS: Record<DepartmentType, string> = {
  common: '공통',
  manufacturing: '제조',
  admin: '관리',
};

// ===== 계정과목 인터페이스 =====
export interface AccountSubject {
  id: string;
  code: string;
  name: string;
  category: AccountSubjectCategory;
  subCategory: string | null;
  parentCode: string | null;
  depth: number;
  departmentType: DepartmentType;
  description: string | null;
  sortOrder: number;
  isActive: boolean;
}

export interface AccountSubjectApiData {
  id: number;
  code: string;
  name: string;
  category: string;
  sub_category: string | null;
  parent_code: string | null;
  depth: number;
  department_type: string;
  description: string | null;
  sort_order: number;
  is_active: boolean | number;
  created_at: string;
  updated_at: string;
}

// ===== API → Frontend 변환 =====
export function transformAccountSubjectApi(apiData: AccountSubjectApiData): AccountSubject {
  return {
    id: String(apiData.id),
    code: apiData.code,
    name: apiData.name,
    category: apiData.category as AccountSubjectCategory,
    subCategory: apiData.sub_category,
    parentCode: apiData.parent_code,
    depth: apiData.depth ?? 3,
    departmentType: (apiData.department_type || 'common') as DepartmentType,
    description: apiData.description,
    sortOrder: apiData.sort_order ?? 0,
    isActive: Boolean(apiData.is_active),
  };
}

// ===== 표시용 포맷 =====
export function formatAccountLabel(subject: AccountSubject): string {
  return `[${subject.code}] ${subject.name}`;
}
