const BaseModel = require('./BaseModel');
const { Model } = require('objection');

class Alias extends BaseModel {
  static get tableName() {
    return 'alias';
  }

  static get idColumn() {
    return 'id';
  }

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['address', 'goto', 'domain'],
      
      properties: {
        id: { type: 'integer' },
        address: { type: 'string', minLength: 1, maxLength: 255 },
        goto: { type: 'string', minLength: 1 },
        domain: { type: 'string', minLength: 1, maxLength: 255 },
        created: { type: 'string', format: 'date-time' },
        modified: { type: 'string', format: 'date-time' },
        active: { type: 'integer', default: 1 },
        ignore: { type: 'integer', default: 0 }
      }
    };
  }

  static get relationMappings() {
    const Domain = require('./Domain');
    
    return {
      domainRelation: {
        relation: Model.BelongsToOneRelation,
        modelClass: Domain,
        join: {
          from: 'alias.domain',
          to: 'domain.domain'
        }
      }
    };
  }
}

module.exports = Alias;