import 'package:superport_v2/features/approvals/domain/entities/approval.dart';

/// 재고 트랜잭션 도메인 엔티티
///
/// - 입고/출고/대여 공통으로 사용되는 헤더와 라인, 고객 연결 정보를 포함한다.
class StockTransaction {
  StockTransaction({
    this.id,
    required this.transactionNo,
    required this.transactionDate,
    required this.type,
    required this.status,
    required this.warehouse,
    required this.createdBy,
    this.note,
    this.isActive = true,
    this.createdAt,
    this.updatedAt,
    this.lines = const [],
    this.customers = const [],
    this.approval,
    this.expectedReturnDate,
  });

  final int? id;
  final String transactionNo;
  final DateTime transactionDate;
  final StockTransactionType type;
  final StockTransactionStatus status;
  final StockTransactionWarehouse warehouse;
  final StockTransactionEmployee createdBy;
  final String? note;
  final bool isActive;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<StockTransactionLine> lines;
  final List<StockTransactionCustomer> customers;
  final Approval? approval;
  final DateTime? expectedReturnDate;

  int get itemCount => lines.length;

  int get totalQuantity => lines.fold<int>(
    0,
    (previousValue, line) => previousValue + line.quantity,
  );

  StockTransaction copyWith({
    int? id,
    String? transactionNo,
    DateTime? transactionDate,
    StockTransactionType? type,
    StockTransactionStatus? status,
    StockTransactionWarehouse? warehouse,
    StockTransactionEmployee? createdBy,
    String? note,
    bool? isActive,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<StockTransactionLine>? lines,
    List<StockTransactionCustomer>? customers,
    Approval? approval,
    DateTime? expectedReturnDate,
  }) {
    return StockTransaction(
      id: id ?? this.id,
      transactionNo: transactionNo ?? this.transactionNo,
      transactionDate: transactionDate ?? this.transactionDate,
      type: type ?? this.type,
      status: status ?? this.status,
      warehouse: warehouse ?? this.warehouse,
      createdBy: createdBy ?? this.createdBy,
      note: note ?? this.note,
      isActive: isActive ?? this.isActive,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      lines: lines ?? this.lines,
      customers: customers ?? this.customers,
      approval: approval ?? this.approval,
      expectedReturnDate: expectedReturnDate ?? this.expectedReturnDate,
    );
  }
}

/// 재고 트랜잭션 유형 요약 정보
class StockTransactionType {
  StockTransactionType({required this.id, required this.name});

  final int id;
  final String name;
}

/// 재고 트랜잭션 상태 요약 정보
class StockTransactionStatus {
  StockTransactionStatus({required this.id, required this.name});

  final int id;
  final String name;
}

/// 재고 트랜잭션 작성자 정보
class StockTransactionEmployee {
  StockTransactionEmployee({
    required this.id,
    required this.employeeNo,
    required this.name,
  });

  final int id;
  final String employeeNo;
  final String name;
}

/// 재고 트랜잭션 창고 정보 요약
class StockTransactionWarehouse {
  StockTransactionWarehouse({
    required this.id,
    required this.code,
    required this.name,
    this.zipcode,
    this.addressLine,
  });

  final int id;
  final String code;
  final String name;
  final String? zipcode;
  final String? addressLine;
}

/// 재고 트랜잭션 품목(라인)
class StockTransactionLine {
  StockTransactionLine({
    this.id,
    required this.lineNo,
    required this.product,
    required this.quantity,
    required this.unitPrice,
    this.note,
  });

  final int? id;
  final int lineNo;
  final StockTransactionProduct product;
  final int quantity;
  final double unitPrice;
  final String? note;
}

/// 재고 트랜잭션 품목의 제품 정보 요약
class StockTransactionProduct {
  StockTransactionProduct({
    required this.id,
    required this.code,
    required this.name,
    this.vendor,
    this.uom,
  });

  final int id;
  final String code;
  final String name;
  final StockTransactionVendorSummary? vendor;
  final StockTransactionUomSummary? uom;
}

/// 재고 트랜잭션에 연결된 고객 정보
class StockTransactionCustomer {
  StockTransactionCustomer({this.id, required this.customer, this.note});

  final int? id;
  final StockTransactionCustomerSummary customer;
  final String? note;
}

/// 고객 요약 정보
class StockTransactionCustomerSummary {
  StockTransactionCustomerSummary({
    required this.id,
    required this.code,
    required this.name,
  });

  final int id;
  final String code;
  final String name;
}

/// 제품의 공급사 요약 정보
class StockTransactionVendorSummary {
  StockTransactionVendorSummary({required this.id, required this.name});

  final int id;
  final String name;
}

/// 제품 단위 요약 정보
class StockTransactionUomSummary {
  StockTransactionUomSummary({required this.id, required this.name});

  final int id;
  final String name;
}

extension StockTransactionLineX on List<StockTransactionLine> {
  /// 라인 품목 가격 총액을 계산한다.
  double get totalAmount =>
      fold<double>(0, (sum, line) => sum + (line.quantity * line.unitPrice));
}
