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

/**
 * Controlador de actualización parcial/total
 */
export class Update{{pascalCase name}}Controller extends ExpressController {
  public constructor(private readonly useCase: Update{{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");

    const { {{snakeCase name}}_id } = this.req.params as { {{snakeCase name}}_id: string };

    // TODO: descomentar cuando exista DTO
    // const validation = validateRequest(Update{{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({ {{snakeCase name}}_id, companyId, ...dto });

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