// 설비 등록대장 타입 정의

// ===== 설비 상태 =====
export type EquipmentStatus = 'active' | 'idle' | 'disposed';

export const EQUIPMENT_STATUS_LABEL: Record<EquipmentStatus, string> = {
  active: '가동',
  idle: '유휴',
  disposed: '폐기',
};

export const EQUIPMENT_STATUS_COLOR: Record<EquipmentStatus, string> = {
  active: 'bg-green-100 text-green-700',
  idle: 'bg-yellow-100 text-yellow-700',
  disposed: 'bg-red-100 text-red-700',
};

// ===== 점검 주기 =====
export type InspectionCycle = 'daily' | 'weekly' | 'monthly' | 'bimonthly' | 'quarterly' | 'semiannual';

export const INSPECTION_CYCLE_LABEL: Record<InspectionCycle, string> = {
  daily: '일일',
  weekly: '주간',
  monthly: '월간',
  bimonthly: '2개월',
  quarterly: '분기',
  semiannual: '반년',
};

export const INSPECTION_CYCLES: InspectionCycle[] = [
  'daily', 'weekly', 'monthly', 'bimonthly', 'quarterly', 'semiannual',
];

// ===== 점검 결과 =====
export type InspectionResult = 'good' | 'bad' | 'repaired' | null;

export const INSPECTION_RESULT_SYMBOL: Record<string, string> = {
  good: '○',
  bad: 'X',
  repaired: '△',
};

// ===== 보전 구분 =====
export type RepairType = 'internal' | 'external';

export const REPAIR_TYPE_LABEL: Record<RepairType, string> = {
  internal: '사내',
  external: '외주',
};

export const REPAIR_TYPE_COLOR: Record<RepairType, string> = {
  internal: 'bg-yellow-100 text-yellow-700',
  external: 'bg-blue-100 text-blue-700',
};

// ===== API 응답 타입 =====

export interface EquipmentApiData {
  id: number;
  tenant_id: number;
  equipment_code: string;
  name: string;
  equipment_type: string | null;
  specification: string | null;
  manufacturer: string | null;
  model_name: string | null;
  serial_no: string | null;
  location: string | null;
  production_line: string | null;
  purchase_date: string | null;
  install_date: string | null;
  purchase_price: string | null;
  useful_life: number | null;
  status: EquipmentStatus;
  disposed_date: string | null;
  manager_id: number | null;
  sub_manager_id: number | null;
  memo: string | null;
  is_active: boolean;
  sort_order: number;
  manager: { id: number; name: string } | null;
  subManager: { id: number; name: string } | null;
  photos?: EquipmentPhotoApi[];
  created_at?: string;
  updated_at?: string;
}

export interface EquipmentPhotoApi {
  id: number;
  display_name: string;
  stored_name: string;
  file_path: string;
  file_size: number;
  mime_type: string;
  created_at: string;
}

export interface InspectionTemplateApi {
  id: number;
  equipment_id: number;
  inspection_cycle: InspectionCycle;
  item_no: string;
  check_point: string;
  check_item: string;
  check_timing: string | null;
  check_frequency: string | null;
  check_method: string | null;
  sort_order: number;
  is_active: boolean;
}

export interface EquipmentRepairApi {
  id: number;
  tenant_id: number;
  equipment_id: number;
  repair_date: string;
  repair_type: RepairType | null;
  repair_hours: number | null;
  description: string | null;
  cost: string | null;
  vendor: string | null;
  repaired_by: number | null;
  memo: string | null;
  equipment: { id: number; equipment_code: string; name: string } | null;
  repairer: { id: number; name: string } | null;
  created_at?: string;
}

export interface EquipmentOptionsApi {
  equipment_types: string[];
  production_lines: string[];
  statuses: Record<EquipmentStatus, string>;
  equipment_list: Array<{
    id: number;
    equipment_code: string;
    name: string;
    equipment_type: string;
    production_line: string;
  }>;
}

export interface EquipmentStatsApi {
  total: number;
  active: number;
  idle: number;
  disposed: number;
  inspection_stats?: {
    target_count: number;
    completed_count: number;
    issue_count: number;
  };
  type_distribution?: Array<{
    equipment_type: string;
    count: number;
  }>;
}

// ===== 프론트엔드 타입 =====

export interface Equipment {
  id: string;
  equipmentCode: string;
  name: string;
  equipmentType: string;
  specification: string;
  manufacturer: string;
  modelName: string;
  serialNo: string;
  location: string;
  productionLine: string;
  purchaseDate: string;
  installDate: string;
  purchasePrice: string;
  usefulLife: number | null;
  status: EquipmentStatus;
  disposedDate: string;
  managerId: number | null;
  subManagerId: number | null;
  managerName: string;
  subManagerName: string;
  memo: string;
  isActive: boolean;
  sortOrder: number;
  photos: EquipmentPhoto[];
}

export interface EquipmentPhoto {
  id: number;
  displayName: string;
  filePath: string;
  fileSize: number;
  mimeType: string;
  createdAt: string;
}

export interface InspectionTemplate {
  id: number;
  equipmentId: number;
  inspectionCycle: InspectionCycle;
  itemNo: string;
  checkPoint: string;
  checkItem: string;
  checkTiming: string;
  checkFrequency: string;
  checkMethod: string;
  sortOrder: number;
  isActive: boolean;
}

export interface EquipmentRepair {
  id: string;
  equipmentId: number;
  repairDate: string;
  repairType: RepairType | null;
  repairHours: number | null;
  description: string;
  cost: string;
  vendor: string;
  repairedBy: number | null;
  repairerName: string;
  memo: string;
  equipmentCode: string;
  equipmentName: string;
}

export interface EquipmentOptions {
  equipmentTypes: string[];
  productionLines: string[];
  statuses: Record<EquipmentStatus, string>;
  equipmentList: Array<{
    id: number;
    equipmentCode: string;
    name: string;
    equipmentType: string;
    productionLine: string;
  }>;
}

export interface EquipmentStats {
  total: number;
  active: number;
  idle: number;
  disposed: number;
  inspectionStats?: {
    targetCount: number;
    completedCount: number;
    issueCount: number;
  };
  typeDistribution?: Array<{
    equipmentType: string;
    count: number;
  }>;
}

// ===== 폼 데이터 =====

export interface EquipmentFormData {
  equipmentCode: string;
  name: string;
  equipmentType: string;
  specification: string;
  manufacturer: string;
  modelName: string;
  serialNo: string;
  location: string;
  productionLine: string;
  purchaseDate: string;
  installDate: string;
  purchasePrice: string;
  usefulLife: string;
  status: EquipmentStatus;
  managerId: string;
  subManagerId: string;
  memo: string;
}

export interface RepairFormData {
  equipmentId: string;
  repairDate: string;
  repairType: string;
  repairHours: string;
  description: string;
  cost: string;
  vendor: string;
  repairedBy: string;
  memo: string;
}

export interface InspectionTemplateFormData {
  inspectionCycle: InspectionCycle;
  itemNo: string;
  checkPoint: string;
  checkItem: string;
  checkTiming: string;
  checkFrequency: string;
  checkMethod: string;
}

// ===== 점검 그리드 =====

export interface InspectionGridRow {
  equipment: {
    id: number;
    equipmentCode: string;
    name: string;
  };
  templates: InspectionTemplate[];
  details: Record<string, InspectionResult>; // key: "{templateId}_{date}"
  canInspect: boolean;
  overallJudgment: string | null;
}

export interface InspectionGridData {
  rows: InspectionGridRow[];
  labels: string[]; // 날짜 라벨 배열
  nonWorkingDays: string[]; // 주말/공휴일 날짜
}

// ===== 직원 옵션 (관리자 선택용) =====

export interface ManagerOption {
  id: string;
  name: string;
  department: string;
  position: string;
}

// ===== 페이지네이션 =====

export interface PaginationMeta {
  currentPage: number;
  lastPage: number;
  perPage: number;
  total: number;
}

// ===== 필터 =====

export interface EquipmentFilter {
  search: string;
  status: EquipmentStatus | 'all';
  productionLine: string;
  equipmentType: string;
}

export interface RepairFilter {
  search: string;
  equipmentId: string;
  repairType: string;
  dateFrom: string;
  dateTo: string;
}
