import CRUDController from "./crud_controller.js";
import vine from '@vinejs/vine'
import { DateTime } from 'luxon'

export default class [MODEL]Controller extends CrudController
{
  declare __request: any;
  declare __response: any;
  declare __params: any;
  declare __data: any;

  constructor(){
      super('[MODEL]')
      this.__data['page_title'] = '[MODEL]';
      this.__indexView  = '[MODEL].index';
      this.__createView = '[MODEL].add';
      this.__editView   = '[MODEL].edit';
      this.__routeName    = '[MODEL]';
      this.__request; //adonis request obj
      this.__response; //adonis response obj
      this.__params = {}; // this is used for get parameters from url
  }

  /**
   * Validates the http request payload
   * @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;
      }
  }

  /**
   * Validates the creation action
   * @returns
   */
  private async _storeValidation()
  {
      let validator: any;
      try {
        const createPostValidator = vine.compile(
          vine.object({
            name: vine.string().trim().minLength(6),
          })
        )
        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;
  }

  /**
   * Validates the creation action
   * @returns
   */
  private async _updateValidation(slug:string)
  {

  }

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

  }

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

  }

  protected async dataTableRecords(record:any)
  {
    let options  = `<a href="/admin/faq/${record.slug}/edit" title="edit" class="btn btn-sm btn-info"><i class="fa fa-edit"></i></a>`;
        options += '<a title="Delete" class="btn btn-sm btn-danger _delete_record"><i class="fa fa-trash"></i></a>';
    return [
      `<input type="checkbox" name="record_id[]" class="record_id" value="${record.slug}"></input>`,
        record.question,
        DateTime.fromISO(record.created_at).toFormat('MM-dd-yyyy'),
        options
    ];
  }

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

  }

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

  }

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

  }

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

  }

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

  }

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

  }

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

  }

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

  }

}
