import 'package:superport/utils/constants.dart';

// 장비 정보 모델
class Equipment {
  final int? id;
  final String manufacturer;
  final String name;
  final String category;
  final String subCategory;
  final String subSubCategory;
  final String? serialNumber;
  final String? barcode;
  final int quantity;
  final DateTime? inDate;
  final String? remark; // 비고
  final String? warrantyLicense; // 워런티 라이센스 명
  DateTime? warrantyStartDate; // 워런티 시작일(수정 가능)
  DateTime? warrantyEndDate; // 워런티 종료일(수정 가능)

  Equipment({
    this.id,
    required this.manufacturer,
    required this.name,
    required this.category,
    required this.subCategory,
    required this.subSubCategory,
    this.serialNumber,
    this.barcode,
    required this.quantity,
    this.inDate,
    this.remark,
    this.warrantyLicense,
    this.warrantyStartDate,
    this.warrantyEndDate,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'manufacturer': manufacturer,
      'name': name,
      'category': category,
      'subCategory': subCategory,
      'subSubCategory': subSubCategory,
      'serialNumber': serialNumber,
      'barcode': barcode,
      'quantity': quantity,
      'inDate': inDate?.toIso8601String(),
      'remark': remark,
      'warrantyLicense': warrantyLicense,
      'warrantyStartDate': warrantyStartDate?.toIso8601String(),
      'warrantyEndDate': warrantyEndDate?.toIso8601String(),
    };
  }

  factory Equipment.fromJson(Map<String, dynamic> json) {
    return Equipment(
      id: json['id'],
      manufacturer: json['manufacturer'],
      name: json['name'],
      category: json['category'],
      subCategory: json['subCategory'],
      subSubCategory: json['subSubCategory'],
      serialNumber: json['serialNumber'],
      barcode: json['barcode'],
      quantity: json['quantity'],
      inDate: json['inDate'] != null ? DateTime.parse(json['inDate']) : null,
      remark: json['remark'],
      warrantyLicense: json['warrantyLicense'],
      warrantyStartDate:
          json['warrantyStartDate'] != null
              ? DateTime.parse(json['warrantyStartDate'])
              : null,
      warrantyEndDate:
          json['warrantyEndDate'] != null
              ? DateTime.parse(json['warrantyEndDate'])
              : null,
    );
  }
}

class EquipmentIn {
  final int? id;
  final Equipment equipment;
  final DateTime inDate;
  final String status; // I (입고)
  final String type; // 장비 유형: '신제품', '중고', '계약'
  final String? warehouseLocation; // 입고지
  final String? partnerCompany; // 파트너사
  final String? remark; // 비고

  EquipmentIn({
    this.id,
    required this.equipment,
    required this.inDate,
    this.status = 'I',
    this.type = EquipmentType.new_, // 기본값은 '신제품'으로 설정
    this.warehouseLocation,
    this.partnerCompany,
    this.remark,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'equipment': equipment.toJson(),
      'inDate': inDate.toIso8601String(),
      'status': status,
      'type': type,
      'warehouseLocation': warehouseLocation,
      'partnerCompany': partnerCompany,
      'remark': remark,
    };
  }

  factory EquipmentIn.fromJson(Map<String, dynamic> json) {
    return EquipmentIn(
      id: json['id'],
      equipment: Equipment.fromJson(json['equipment']),
      inDate: DateTime.parse(json['inDate']),
      status: json['status'],
      type: json['type'] ?? EquipmentType.new_,
      warehouseLocation: json['warehouseLocation'],
      partnerCompany: json['partnerCompany'],
      remark: json['remark'],
    );
  }
}

class EquipmentOut {
  final int? id;
  final Equipment equipment;
  final DateTime outDate;
  final String status; // O (출고), I (재입고), R (수리)
  final String? company; // 출고 회사
  final String? manager; // 담당자
  final String? license; // 라이센스
  final DateTime? returnDate; // 재입고/수리 날짜
  final String? returnType; // 재입고/수리 유형
  final String? remark; // 비고

  EquipmentOut({
    this.id,
    required this.equipment,
    required this.outDate,
    this.status = 'O',
    this.company,
    this.manager,
    this.license,
    this.returnDate,
    this.returnType,
    this.remark,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'equipment': equipment.toJson(),
      'outDate': outDate.toIso8601String(),
      'status': status,
      'company': company,
      'manager': manager,
      'license': license,
      'returnDate': returnDate?.toIso8601String(),
      'returnType': returnType,
      'remark': remark,
    };
  }

  factory EquipmentOut.fromJson(Map<String, dynamic> json) {
    return EquipmentOut(
      id: json['id'],
      equipment: Equipment.fromJson(json['equipment']),
      outDate: DateTime.parse(json['outDate']),
      status: json['status'],
      company: json['company'],
      manager: json['manager'],
      license: json['license'],
      returnDate:
          json['returnDate'] != null
              ? DateTime.parse(json['returnDate'])
              : null,
      returnType: json['returnType'],
      remark: json['remark'],
    );
  }
}

class UnifiedEquipment {
  final int? id;
  final Equipment equipment;
  final DateTime date; // 입고일 또는 출고일
  final String
  status; // 상태 코드: 'I'(입고), 'O'(출고), 'R'(수리중), 'D'(손상), 'L'(분실), 'E'(기타)
  final String? notes; // 추가 비고
  final String? _type; // 내부용: 입고 장비 유형

  UnifiedEquipment({
    this.id,
    required this.equipment,
    required this.date,
    required this.status,
    this.notes,
    String? type,
  }) : _type = type;

  // 장비 유형 반환 (입고 장비만)
  String? get type => status == 'I' ? _type : null;

  // 장비 상태 텍스트 변환
  String get statusText {
    switch (status) {
      case EquipmentStatus.in_:
        return '입고';
      case EquipmentStatus.out:
        return '출고';
      case EquipmentStatus.rent:
        return '대여';
      case EquipmentStatus.repair:
        return '수리중';
      case EquipmentStatus.damaged:
        return '손상';
      case EquipmentStatus.lost:
        return '분실';
      case EquipmentStatus.etc:
        return '기타';
      default:
        return '알 수 없음';
    }
  }

  // EquipmentIn 모델에서 변환
  factory UnifiedEquipment.fromEquipmentIn(
    id,
    equipment,
    inDate,
    status, {
    String? type,
  }) {
    return UnifiedEquipment(
      id: id,
      equipment: equipment,
      date: inDate,
      status: status,
      type: type,
    );
  }

  // EquipmentOut 모델에서 변환
  factory UnifiedEquipment.fromEquipmentOut(id, equipment, outDate, status) {
    return UnifiedEquipment(
      id: id,
      equipment: equipment,
      date: outDate,
      status: status,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'equipment': equipment.toJson(),
      'date': date.toIso8601String(),
      'status': status,
      'notes': notes,
    };
  }

  factory UnifiedEquipment.fromJson(Map<String, dynamic> json) {
    return UnifiedEquipment(
      id: json['id'],
      equipment: Equipment.fromJson(json['equipment']),
      date: DateTime.parse(json['date']),
      status: json['status'],
      notes: json['notes'],
    );
  }
}
