import { BaseProvider } from "#templates/base/provider";
import { {{ modelUpperCase }} } from "#models/{{ modelUpperCase }}";
import { {{ modelUpperCase }}Attributes, {{ modelUpperCase }}CreationAttributes } from "#models/{{ modelUpperCase }}";
import { Errors } from "#interfaces/error";

export class {{ modelUpperCase }}Provider extends BaseProvider<{{ modelUpperCase }}> {
  constructor(skipInitDb?: boolean) {
    super("{{ modelUpperCase }}", skipInitDb);
  }

  async createWithValidation(data: {{ modelUpperCase }}CreationAttributes): Promise<{{ modelUpperCase }}> {
    try {
      if (!data) throw Errors.invalidInput();
      return this.create(data);
    } catch (error) {
      await this.logError(error as Error);
      throw Errors.databaseError();
    }
  }

  async updateWithValidation(id: string, data: Partial<{{ modelUpperCase }}Attributes>): Promise<{{ modelUpperCase }} | null> {
    try {
      if (!id) throw Errors.invalidInput();
      return this.updateById(id, data);
    } catch (error) {
      await this.logError(error as Error);
      throw Errors.databaseError();
    }
  }

{{#hasStatus}}
  async findByStatus(status: string): Promise<{{ modelUpperCase }}[]> {
    try {
      return this.findAll({ where: { status } as any });
    } catch (error) {
      await this.logError(error as Error);
      throw Errors.databaseError();
    }
  }
{{/hasStatus}}

{{#hasIsDeleted}}
  async softDelete(id: string): Promise<boolean> {
    try {
      if (!id) throw Errors.invalidInput();
      const result = await this.updateById(id, { isDeleted: true } as any);
      return !!result;
    } catch (error) {
      await this.logError(error as Error);
      throw Errors.databaseError();
    }
  }
{{/hasIsDeleted}}

{{#hasIsActive}}
  async findActive(): Promise<{{ modelUpperCase }}[]> {
    try {
      return this.findAll({ where: { isActive: true } as any });
    } catch (error) {
      await this.logError(error as Error);
      throw Errors.databaseError();
    }
  }
{{/hasIsActive}}
}