import { ExpressController, authGuard, tenantGuard, forbidQueryFieldGuard } from "@erp/core/api";
import { Create{{pascalCase name}}UseCase } from "../../../application";

/**
 * Controlador de creación
 * @remarks
 * - Aplica guards de autenticación, tenant y prohíbe `companyId` en query.
 */
export class Create{{pascalCase name}}Controller extends ExpressController {
  public constructor(private readonly useCase: Create{{pascalCase name}}UseCase) {
    super();
    this.useGuards(authGuard(), tenantGuard(), forbidQueryFieldGuard("companyId"));
  }

  protected async executeImpl() {
    const companyId = this.getTenantId();
    if (!companyId) return this.forbiddenError("Tenant ID not found");

    // TODO: descomentar cuando exista DTO
    // const validation = validateRequest(Create{{pascalCase name}}Dto, this.req.body);
    // if (validation.isErr()) return this.badRequestError(validation.error.message);
    // const dto = validation.value;

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

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