import { Model, mixin } from 'objection';
import TenantModel from '@/models/TenantModel';
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
import CachableModel from '@/lib/Cachable/CachableModel';

export default class View extends mixin(TenantModel, [CachableModel]) {
  /**
   * Table name.
   */
  static get tableName() {
    return 'views';
  }

  /**
   * Extend query builder model.
   */
  static get QueryBuilder() {
    return CachableQueryBuilder;
  }

  /**
   * Relationship mapping.
   */
  static get relationMappings() {
    const Resource = require('@/models/Resource');
    const ViewColumn = require('@/models/ViewColumn');
    const ViewRole = require('@/models/ViewRole');

    return {
      /**
       * View model belongs to resource model.
       */
      resource: {
        relation: Model.BelongsToOneRelation,
        modelClass: this.relationBindKnex(Resource.default),
        join: {
          from: 'views.resourceId',
          to: 'resources.id',
        },
      },

      /**
       * View model may has many columns.
       */
      columns: {
        relation: Model.HasManyRelation,
        modelClass: this.relationBindKnex(ViewColumn.default),
        join: {
          from: 'views.id',
          to: 'view_has_columns.viewId',
        },
      },

      /**
       * View model may has many view roles.
       */
      roles: {
        relation: Model.HasManyRelation,
        modelClass: this.relationBindKnex(ViewRole.default),
        join: {
          from: 'views.id',
          to: 'view_roles.viewId',
        },
      },
    };
  }
}
