import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '../config/database';
import type { MeetingProvider } from '../types/meeting.types';

interface ExamMeetingAttributes {
  id: number;
  course_id: string;
  recid: string;
  itemid: string;
  meeting_id: string;
  provider: MeetingProvider;
  created_by: string;
  is_active: boolean;
  createdAt?: Date;
  updatedAt?: Date;
}

interface ExamMeetingCreationAttributes extends Optional<ExamMeetingAttributes, 'id' | 'is_active' | 'createdAt' | 'updatedAt'> {}

class ExamMeeting extends Model<ExamMeetingAttributes, ExamMeetingCreationAttributes> implements ExamMeetingAttributes {
  public id!: number;
  public course_id!: string;
  public recid!: string;
  public itemid!: string;
  public meeting_id!: string;
  public provider!: MeetingProvider;
  public created_by!: string;
  public is_active!: boolean;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

ExamMeeting.init(
  {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      autoIncrement: true,
      primaryKey: true,
    },
    course_id: {
      type: DataTypes.STRING(50),
      allowNull: false,
    },
    recid: {
      type: DataTypes.STRING(50),
      allowNull: false,
    },
    itemid: {
      type: DataTypes.STRING(50),
      allowNull: false,
    },
    meeting_id: {
      type: DataTypes.STRING(120),
      allowNull: false,
    },
    provider: {
      type: DataTypes.ENUM('videosdk', 'livekit'),
      allowNull: false,
      defaultValue: 'livekit',
    },
    created_by: {
      type: DataTypes.STRING(50),
      allowNull: false,
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
  },
  {
    sequelize,
    tableName: 'exam_meetings',
    timestamps: true,
    underscored: true,
    indexes: [
      {
        unique: true,
        fields: ['course_id', 'recid', 'itemid'],
        name: 'unique_exam_meeting',
      },
      {
        fields: ['meeting_id'],
      },
      {
        fields: ['is_active'],
      },
    ],
  }
);

export default ExamMeeting;
