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

export class CreateCustomerController extends ExpressController {
  public constructor(private readonly useCase: CreateCustomerUseCase) {
    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 CreateCustomerRequestDTO;

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

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