import RestController from './rest_controller.js'
import vine from '@vinejs/vine'
import { regexRule } from '#rules/regex'

export default class [MODEL]Controller extends RestController
{
  protected __resource: string;
  declare __request:any;
  declare __params: any;

  constructor() {
      super("[MODEL]");
      this.__resource = "[MODEL]";
  }

  /**
    *
    * @param action
    * @param slug
    */
  protected async validation( action: string, slug: string )
  {
      switch (action) {
        case "store":
          await this._storeValidation();
        break;
        case "update":
          await this._updateValidation(slug);
        break;
      }
  }

  /**
    *
    * @returns
    */
  private async _storeValidation()
  {
      let validator: any;
      try {
        const createPostValidator = vine.compile(
          vine.object({
            name: vine.string().trim().minLength(3).maxLength(50),
          })
        )
        validator = await createPostValidator.validate(this.__request.all())
      } catch (error) {
        this.__is_error = true;
        let messages = await this.__validate(error)
        return this.__sendError(messages);
      }
      return validator;
  }

  /**
    *
    * @returns
    */
  private async _updateValidation(slug:string)
  {
      let validator: any;
      try {
        const createPostValidator = vine.compile(
          vine.object({
            name: vine.string().trim().minLength(3).maxLength(50),
          })
        )
        validator = await createPostValidator.validate(this.__request.all())
      } catch (error) {
        this.__is_error = true;
        let messages = await this.__validate(error)
        return this.__sendError(messages);
      }
      return validator;
  }

  /**
    *
    */
  protected async beforeIndexLoadModel()
  {

  }

  /**
    *
    */
  protected async afterIndexLoadModel(records:any)
  {

  }

  /**
    *
    */
  protected async beforeStoreLoadModel()
  {

  }

  /**
    *
    */
  protected async afterStoreLoadModel()
  {

  }

  /**
    *
    */
  protected async beforeShowLoadModel()
  {

  }

  /**
    *
    */
  protected async afterShowLoadModel(record: any)
  {

  }

  /**
    *
    */
  protected async beforeUpdateLoadModel()
  {

  }

  /**
    *
    */
  protected async afterUpdateLoadModel()
  {

  }

  /**
    *
    */
  protected async beforeDestroyLoadModel()
  {

  }

  /**
    *
    */
  protected async afterDestroyLoadModel()
  {

  }
}
