import { ExpressController, authGuard, forbidQueryFieldGuard, tenantGuard } from "@erp/core/api";

import { CreateCustomerInvoiceRequestDTO } from "../../../../common/dto";
import { CreateCustomerInvoiceUseCase } from "../../../application";

export class CreateCustomerInvoiceController extends ExpressController {
  public constructor(private readonly useCase: CreateCustomerInvoiceUseCase) {
    super();
    // 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
    this.useGuards(authGuard(), tenantGuard(), forbidQueryFieldGuard("companyId"));
  }

  protected async executeImpl() {
    const companyId = this.getTenantId();
    if (!companyId) {
      return this.forbiddenError("Tenant ID not found");
    }
    const dto = this.req.body as CreateCustomerInvoiceRequestDTO;

    const result = await this.useCase.execute({ dto, companyId });

    return result.match(
      (data) => this.created(data),
      (err) => this.handleError(err)
    );
  }
}
