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

/**
 * CREATE TABLE `domain_admins` (
  `username` varchar(255) NOT NULL,
  `domain` varchar(255) NOT NULL,
  `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`),
  KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Postfix Admin - Domain Admins'
 */

class DomainAdmin extends BaseModel {
    static get tableName() {
      return 'domain_admins';
    }
  
    static get idColumn() {
      return 'id';
    }
  
    static get jsonSchema() {
      return {
        type: 'object',
        required: ['username', 'domain'],
        
        properties: {
          id: { type: 'integer' },
          username: { type: 'string', minLength: 1, maxLength: 255 },
          domain: { type: 'string', minLength: 1, maxLength: 255 },
          created: { type: 'string', format: 'date-time' },
          active: { type: 'integer', default: 1 },
        }
      };
    }
  
    static get relationMappings() {
      const Domain = require('./Domain');
      
      return {
        mailboxes: {
          relation: Model.HasManyRelation,
          modelClass: Domain,
          join: {
            from: 'domain_admins.domain',
            to: 'domain.domain'
          }
        }
      };
    }
  }
  
  module.exports = DomainAdmin;