import 'package:superport_v2/features/inventory/transactions/domain/entities/stock_transaction.dart';

/// 출고 화면에서 사용하는 UI용 레코드 모델.
class OutboundRecord {
  OutboundRecord({
    this.id,
    required this.number,
    required this.transactionNumber,
    required this.transactionType,
    this.transactionTypeId,
    required this.processedAt,
    required this.warehouse,
    this.warehouseId,
    this.warehouseCode,
    this.warehouseZipcode,
    this.warehouseAddress,
    required this.status,
    this.statusId,
    required this.writer,
    this.writerId,
    required this.remark,
    required this.items,
    required this.customers,
    this.raw,
  });

  factory OutboundRecord.fromTransaction(StockTransaction transaction) {
    return OutboundRecord(
      id: transaction.id,
      number: transaction.transactionNo,
      transactionNumber: transaction.transactionNo,
      transactionType: transaction.type.name,
      transactionTypeId: transaction.type.id,
      processedAt: transaction.transactionDate,
      warehouse: transaction.warehouse.name,
      warehouseId: transaction.warehouse.id,
      warehouseCode: transaction.warehouse.code,
      warehouseZipcode: transaction.warehouse.zipcode,
      warehouseAddress: transaction.warehouse.addressLine,
      status: transaction.status.name,
      statusId: transaction.status.id,
      writer: transaction.createdBy.name,
      writerId: transaction.createdBy.id,
      remark: transaction.note ?? '-',
      items: transaction.lines
          .map(OutboundLineItem.fromLine)
          .toList(growable: false),
      customers: transaction.customers
          .map(OutboundCustomer.fromLink)
          .toList(growable: false),
      raw: transaction,
    );
  }

  final int? id;
  final String number;
  final String transactionNumber;
  final String transactionType;
  final int? transactionTypeId;
  final DateTime processedAt;
  final String warehouse;
  final int? warehouseId;
  final String? warehouseCode;
  final String? warehouseZipcode;
  final String? warehouseAddress;
  final String status;
  final int? statusId;
  final String writer;
  final int? writerId;
  final String remark;
  final List<OutboundLineItem> items;
  final List<OutboundCustomer> customers;
  final StockTransaction? raw;

  int get customerCount => customers.length;

  int get itemCount => items.length;

  int get totalQuantity =>
      items.fold<int>(0, (sum, item) => sum + item.quantity);

  double get totalAmount =>
      items.fold<double>(0, (sum, item) => sum + (item.price * item.quantity));
}

/// 출고 상세 모달에 표시되는 품목 정보.
class OutboundLineItem {
  OutboundLineItem({
    this.id,
    int? lineNo,
    int? productId,
    String? productCode,
    required this.product,
    required this.manufacturer,
    required this.unit,
    required this.quantity,
    required this.price,
    required this.remark,
  }) : lineNo = lineNo ?? 0,
       productId = productId ?? 0,
       productCode = productCode ?? '';

  factory OutboundLineItem.fromLine(StockTransactionLine line) {
    final product = line.product;
    return OutboundLineItem(
      id: line.id,
      lineNo: line.lineNo,
      productId: product.id,
      productCode: product.code,
      product: product.name,
      manufacturer: product.vendor?.name ?? '-',
      unit: product.uom?.name ?? '-',
      quantity: line.quantity,
      price: line.unitPrice,
      remark: line.note ?? '',
    );
  }

  final int? id;
  final int lineNo;
  final int productId;
  final String productCode;
  final String product;
  final String manufacturer;
  final String unit;
  final int quantity;
  final double price;
  final String remark;
}

/// 출고 고객 연결 요약 정보.
class OutboundCustomer {
  OutboundCustomer({
    this.id,
    required this.customerId,
    required this.code,
    required this.name,
    this.note,
  });

  factory OutboundCustomer.fromLink(StockTransactionCustomer link) {
    final target = link.customer;
    return OutboundCustomer(
      id: link.id,
      customerId: target.id,
      code: target.code,
      name: target.name,
      note: link.note,
    );
  }

  final int? id;
  final int customerId;
  final String code;
  final String name;
  final String? note;
}
