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

class Domain extends BaseModel {
  static get tableName() {
    return 'domain';
  }

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

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['domain', 'description'],
      
      properties: {
        domain: { type: 'string', minLength: 1, maxLength: 255 },
        description: { type: 'string', maxLength: 255 },
        aliases: { type: 'integer', default: 0 },
        mailboxes: { type: 'integer', default: 0 },
        maxquota: { type: 'integer', default: 0 },
        quota: { type: 'integer', default: 0 },
        transport: { type: 'string', maxLength: 255 },
        backupmx: { type: 'integer', default: 0 },
        created: { type: 'string', format: 'date-time' },
        modified: { type: 'string', format: 'date-time' },
        active: { type: 'integer', default: 1 },
        password_expiry: { type: 'integer', default: 0 }
      }
    };
  }

  static get relationMappings() {
    const Mailbox = require('./Mailbox');
    
    return {
      mailboxes: {
        relation: Model.HasManyRelation,
        modelClass: Mailbox,
        join: {
          from: 'domain.domain',
          to: 'mailbox.domain'
        }
      }
    };
  }
}

module.exports = Domain;