import { Address, Order } from '@/domain/entities';
import { OrderDomainService } from '@/domain/services/OrderDomainService';
import { ICartRepository } from '@/application/ports/ICartRepository';
import { IAuthRepository } from '@/application/ports/IAuthRepository';

export interface PlaceOrderInput {
  deliveryAddress: Address;
  paymentMethod: string;
}

export interface PlaceOrderResult {
  order: Order;
}

/**
 * USE CASE — Checkout: create order, save to auth, clear cart
 */
export class PlaceOrderUseCase {
  constructor(
    private readonly cart: ICartRepository,
    private readonly auth: IAuthRepository
  ) {}

  execute(input: PlaceOrderInput): PlaceOrderResult {
    const items = this.cart.getItems();
    const order = OrderDomainService.createOrder({
      items,
      deliveryAddress: input.deliveryAddress,
      paymentMethod: input.paymentMethod,
    });

    this.auth.addOrder(order);
    this.cart.clearCart();

    return { order };
  }
}