import { Address, CartItem, Order } from '@/domain/entities';
import { DeliveryFeeService } from './DeliveryFeeService';
import { CartDomainService } from './CartDomainService';

export interface CreateOrderInput {
  items: CartItem[];
  deliveryAddress: Address;
  paymentMethod: string;
}

/**
 * DOMAIN SERVICE — Order creation rules
 */
export class OrderDomainService {
  static generateOrderId(): string {
    return `KG-${Date.now().toString(36).toUpperCase()}`;
  }

  static createOrder(input: CreateOrderInput): Order {
    const subtotal = CartDomainService.calculateSubtotal(input.items);
    const total = DeliveryFeeService.calculateGrandTotal(subtotal);

    return {
      id: OrderDomainService.generateOrderId(),
      items: [...input.items],
      total,
      status: 'confirmed',
      createdAt: new Date().toISOString(),
      deliveryAddress: input.deliveryAddress,
      paymentMethod: input.paymentMethod,
    };
  }
}