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

export const LicenseRequestStatus = RequestStatus;
export type LicenseRequestStatus = RequestStatus;

export class LicenseRequest extends BaseModel {
  static tableName = 'license_requests';

  id!: string;
  requestNumber!: string;
  tokenId?: string;
  applicantId!: string;
  requestType!: RequestType;
  workflowId?: string;
  status!: RequestStatus;
  metadata?: Record<string, unknown>;
  currentStageId?: string;
  blockchainTxHash?: string;
  createdAt!: Date;
  updatedAt!: Date;
  submittedAt?: Date;
  approvedAt?: Date;

  // Relations
  applicant?: Model;
  workflow?: Model;
  documents?: Model[];
  approvals?: Model[];
  workflowState?: Model;

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['requestNumber', 'applicantId', 'requestType'],
      properties: {
        id: { type: 'string', format: 'uuid' },
        requestNumber: { type: 'string', maxLength: 50 },
        tokenId: { type: ['string', 'null'] },
        applicantId: { type: 'string', format: 'uuid' },
        requestType: { type: 'string', maxLength: 100 },
        workflowId: { type: ['string', 'null'], format: 'uuid' },
        status: { type: 'string', maxLength: 50, default: 'DRAFT' },
        metadata: { type: ['object', 'null'] },
        currentStageId: { type: ['string', 'null'], maxLength: 100 },
        blockchainTxHash: { type: ['string', 'null'], maxLength: 66 },
        createdAt: { type: 'string', format: 'date-time' },
        updatedAt: { type: 'string', format: 'date-time' },
        submittedAt: { type: ['string', 'null'], format: 'date-time' },
        approvedAt: { type: ['string', 'null'], format: 'date-time' },
      },
    };
  }

  static get relationMappings(): RelationMappingsThunk {
    return (): RelationMappings => {
      const { Applicant } = require('./applicant.model');
      const { Workflow } = require('./workflow.model');
      const { Document } = require('./document.model');
      const { Approval } = require('./approval.model');
      const { WorkflowState } = require('./workflow-state.model');

      return {
        applicant: {
          relation: Model.BelongsToOneRelation,
          modelClass: Applicant,
          join: {
            from: 'license_requests.applicant_id',
            to: 'applicants.id',
          },
        },
        workflow: {
          relation: Model.BelongsToOneRelation,
          modelClass: Workflow,
          join: {
            from: 'license_requests.workflow_id',
            to: 'workflows.id',
          },
        },
        documents: {
          relation: Model.HasManyRelation,
          modelClass: Document,
          join: {
            from: 'license_requests.id',
            to: 'documents.request_id',
          },
        },
        approvals: {
          relation: Model.HasManyRelation,
          modelClass: Approval,
          join: {
            from: 'license_requests.id',
            to: 'approvals.request_id',
          },
        },
        workflowState: {
          relation: Model.HasOneRelation,
          modelClass: WorkflowState,
          join: {
            from: 'license_requests.id',
            to: 'workflow_states.request_id',
          },
        },
      };
    };
  }
}
