import { Model, RelationMappings, RelationMappingsThunk } from 'objection';
import { BaseModel } from './base.model';

export class Applicant extends BaseModel {
  static tableName = 'applicants';

  id!: string;
  digilockerId!: string;
  name!: string;
  email!: string;
  phone?: string;
  walletAddress?: string;
  isActive!: boolean;
  firstName?: string;
  lastName?: string;
  departmentCode?: string;
  lastLoginAt?: Date;
  createdAt!: Date;
  updatedAt!: Date;

  // Relations
  requests?: Model[];

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['digilockerId', 'name', 'email'],
      properties: {
        id: { type: 'string', format: 'uuid' },
        digilockerId: { type: 'string', maxLength: 255 },
        name: { type: 'string', maxLength: 255 },
        email: { type: 'string', format: 'email', maxLength: 255 },
        phone: { type: ['string', 'null'], maxLength: 20 },
        walletAddress: { type: ['string', 'null'], maxLength: 42 },
        isActive: { type: 'boolean', default: true },
        firstName: { type: ['string', 'null'] },
        lastName: { type: ['string', 'null'] },
        departmentCode: { type: ['string', 'null'] },
        lastLoginAt: { type: ['string', 'null'], format: 'date-time' },
        createdAt: { type: 'string', format: 'date-time' },
        updatedAt: { type: 'string', format: 'date-time' },
      },
    };
  }

  static get relationMappings(): RelationMappingsThunk {
    return (): RelationMappings => {
      const { LicenseRequest } = require('./license-request.model');
      return {
        requests: {
          relation: Model.HasManyRelation,
          modelClass: LicenseRequest,
          join: {
            from: 'applicants.id',
            to: 'license_requests.applicant_id',
          },
        },
      };
    };
  }
}
