import { Model, DataTypes, Sequelize } from 'sequelize';

export interface TerminationRequestAttributes {
    id: string;
    requestId: string;
    dealerId: string;
    category: string;
    reason: string;
    proposedLwd: Date;
    status: string;
    currentStage: string;
    initiatedBy: string;
    comments: string | null;
    timeline: any[];
    documents: any[];
    departmentalClearances: Record<string, {
        status: 'Pending' | 'Cleared' | 'Dues';
        amount?: number;
        type?: 'Payable' | 'Receivable' | 'Recovery';
        remarks?: string;
        updatedAt?: string;
        updatedBy?: string;
    }> | null;
}

export interface TerminationRequestInstance extends Model<TerminationRequestAttributes>, TerminationRequestAttributes { }

export default (sequelize: Sequelize) => {
    const TerminationRequest = sequelize.define<TerminationRequestInstance>('TerminationRequest', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false
        },
        dealerId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'dealers',
                key: 'id'
            }
        },
        category: {
            type: DataTypes.STRING,
            allowNull: false
        },
        reason: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        proposedLwd: {
            type: DataTypes.DATEONLY,
            allowNull: false
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'Initiated'
        },
        currentStage: {
            type: DataTypes.STRING,
            defaultValue: 'INITIATED'
        },
        initiatedBy: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        comments: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        timeline: {
            type: DataTypes.JSON,
            defaultValue: []
        },
        documents: {
            type: DataTypes.JSON,
            defaultValue: []
        },
        departmentalClearances: {
            type: DataTypes.JSON,
            defaultValue: {}
        }
    }, {
        tableName: 'termination_requests',
        timestamps: true
    });

    (TerminationRequest as any).associate = (models: any) => {
        TerminationRequest.belongsTo(models.Dealer, { foreignKey: 'dealerId', as: 'dealer' });
        TerminationRequest.belongsTo(models.User, { foreignKey: 'initiatedBy', as: 'initiator' });
        TerminationRequest.hasOne(models.FnF, { foreignKey: 'terminationRequestId', as: 'fnfSettlement' });
        TerminationRequest.hasMany(models.TerminationDocument, {
            foreignKey: 'terminationRequestId',
            as: 'uploadedDocuments'
        });
        TerminationRequest.hasMany(models.Worknote, {
            foreignKey: 'requestId',
            as: 'worknotes',
            scope: { requestType: 'termination' },
            constraints: false
        });
        TerminationRequest.hasMany(models.RequestParticipant, {
            foreignKey: 'requestId',
            as: 'participants',
            scope: { requestType: 'termination' },
            constraints: false
        });
    };

    return TerminationRequest;
};
