import { ExpressController, authGuard, forbidQueryFieldGuard, tenantGuard } from "@erp/core/api";
import { UpdateCustomerInvoiceByIdRequestDTO } from "../../../../common/dto";
import { UpdateCustomerInvoiceUseCase } from "../../../application";

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

  async executeImpl(): Promise<any> {
    const companyId = this.getTenantId();
    if (!companyId) {
      return this.forbiddenError("Tenant ID not found");
    }
    const { invoice_id } = this.req.params;
    const dto = this.req.body as UpdateCustomerInvoiceByIdRequestDTO;

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

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