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

/**
 * CREATE TABLE `fetchmail` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `mailbox` varchar(255) NOT NULL,
  `src_server` varchar(255) NOT NULL,
  `src_auth` enum('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `src_user` varchar(255) NOT NULL,
  `src_password` varchar(255) NOT NULL,
  `src_folder` varchar(255) NOT NULL,
  `poll_time` int(11) unsigned NOT NULL DEFAULT 10,
  `fetchall` tinyint(1) unsigned NOT NULL DEFAULT 0,
  `keep` tinyint(1) unsigned NOT NULL DEFAULT 0,
  `protocol` enum('POP3','IMAP','POP2','ETRN','AUTO') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `usessl` tinyint(1) unsigned NOT NULL DEFAULT 0,
  `extra_options` text DEFAULT NULL,
  `returned_text` text DEFAULT NULL,
  `mda` varchar(255) NOT NULL,
  `date` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00',
  `sslcertck` tinyint(1) NOT NULL DEFAULT 0,
  `sslcertpath` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT '',
  `sslfingerprint` varchar(255) DEFAULT '',
  `domain` varchar(255) DEFAULT '',
  `active` tinyint(1) NOT NULL DEFAULT 0,
  `created` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00',
  `modified` timestamp NOT NULL DEFAULT current_timestamp(),
  `src_port` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
 */

class FetchMail extends BaseModel {
    static get tableName() {
      return 'fetchmail';
    }
  
    static get idColumn() {
      return 'id';
    }
  
    static get jsonSchema() {
      return {
        type: 'object',
        required: ['mailbox', 'src_server', 'src_user', 'src_password', 'src_folder', 'mda', 'date'],
        
        properties: {
          id: { type: 'integer' },
          mailbox: { type: 'string', minLength: 1, maxLength: 255 },
          src_server: { type: 'string', minLength: 1, maxLength: 255 },
          src_auth: { type: 'string', maxLength: 255 },
          src_user: { type: 'string', minLength: 1, maxLength: 255 },
          src_password: { type: 'string', minLength: 1, maxLength: 255 },
          src_folder: { type: 'string', minLength: 1, maxLength: 255 },
          poll_time: { type: 'integer', default: 10 },
          fetchall: { type: 'integer', default: 0 },
          keep: { type: 'integer', default: 0 },
          protocol: { type: 'string', maxLength: 255 },
          usessl: { type: 'integer', default: 0 },
          extra_options: { type: 'string', maxLength: 255 },
          returned_text: { type: 'string', maxLength: 255 },
          mda: { type: 'string', maxLength: 255 },
          date: { type: 'string', format: 'date-time' },
          sslcertck: { type: 'integer', default: 0 },
          sslcertpath: { type: 'string', maxLength: 255 },
          sslfingerprint: { type: 'string', maxLength: 255 },
          domain: { type: 'string', maxLength: 255 },
          active: { type: 'integer', default: 0 },
          created: { type: 'string', format: 'date-time' },
          modified: { type: 'string', format: 'date-time' },
          src_port: { type: 'integer', default: 0 }
        }
      };
    }
  
    static get relationMappings() {
      const Mailbox = require('./Mailbox');
      
      return {
        mailboxes: {
          relation: Model.HasManyRelation,
          modelClass: Mailbox,
          join: {
            from: 'fetchmail.mailbox',
            to: 'mailbox.username'
          }
        }
      };
    }
  }
  
  module.exports = FetchMail;