import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';
import { User } from './User';
import { WorkflowRequest } from './WorkflowRequest';
import { ParticipantType } from '../types/common.types';

interface ParticipantAttributes {
  participantId: string;
  requestId: string;
  userId: string;
  userEmail: string;
  userName: string;
  participantType: ParticipantType;
  canComment: boolean;
  canViewDocuments: boolean;
  canDownloadDocuments: boolean;
  notificationEnabled: boolean;
  addedBy: string;
  addedAt: Date;
  isActive: boolean;
}

interface ParticipantCreationAttributes extends Optional<ParticipantAttributes, 'participantId' | 'addedAt'> {}

class Participant extends Model<ParticipantAttributes, ParticipantCreationAttributes> implements ParticipantAttributes {
  public participantId!: string;
  public requestId!: string;
  public userId!: string;
  public userEmail!: string;
  public userName!: string;
  public participantType!: ParticipantType;
  public canComment!: boolean;
  public canViewDocuments!: boolean;
  public canDownloadDocuments!: boolean;
  public notificationEnabled!: boolean;
  public addedBy!: string;
  public addedAt!: Date;
  public isActive!: boolean;

  // Associations
  public request?: WorkflowRequest;
  public user?: User;
  public addedByUser?: User;
}

Participant.init(
  {
    participantId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'participant_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    userId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'user_id',
      references: {
        model: 'users',
        key: 'user_id'
      }
    },
    userEmail: {
      type: DataTypes.STRING(255),
      allowNull: false,
      field: 'user_email'
    },
    userName: {
      type: DataTypes.STRING(200),
      allowNull: false,
      field: 'user_name'
    },
    participantType: {
      type: DataTypes.ENUM('SPECTATOR', 'INITIATOR', 'APPROVER', 'CONSULTATION'),
      allowNull: false,
      field: 'participant_type'
    },
    canComment: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'can_comment'
    },
    canViewDocuments: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'can_view_documents'
    },
    canDownloadDocuments: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'can_download_documents'
    },
    notificationEnabled: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'notification_enabled'
    },
    addedBy: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'added_by',
      references: {
        model: 'users',
        key: 'user_id'
      }
    },
    addedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'added_at'
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'is_active'
    }
  },
  {
    sequelize,
    modelName: 'Participant',
    tableName: 'participants',
    timestamps: false,
    indexes: [
      {
        fields: ['request_id']
      },
      {
        fields: ['user_id']
      },
      {
        fields: ['participant_type']
      },
      {
        unique: true,
        fields: ['request_id', 'user_id']
      }
    ]
  }
);

// Associations
Participant.belongsTo(WorkflowRequest, {
  as: 'request',
  foreignKey: 'requestId',
  targetKey: 'requestId'
});

Participant.belongsTo(User, {
  as: 'user',
  foreignKey: 'userId',
  targetKey: 'userId'
});

Participant.belongsTo(User, {
  as: 'addedByUser',
  foreignKey: 'addedBy',
  targetKey: 'userId'
});

export { Participant };
