import { Model, DataTypes, Sequelize } from 'sequelize';
import { FNF_STATUS } from '../../../common/config/constants.js';

export interface FnFAttributes {
    id: string;
    settlementId: string | null;
    resignationId: string | null;
    terminationRequestId: string | null;
    outletId: string | null;
    dealerId: string | null; // For direct dealer level F&F
    status: string;
    totalReceivables: number;
    totalPayables: number;
    totalDeductions: number;
    netAmount: number;
    settlementAmount: number | null;
    settlementDate: Date | null;
    paymentMode: string | null;
    transactionReference: string | null;
    remarks: string | null;
    clearanceDocuments: any[];
    progressPercentage: number;
    timeline: any[];
}

export interface FnFInstance extends Model<FnFAttributes>, FnFAttributes { }

export default (sequelize: Sequelize) => {
    const FnF = sequelize.define<FnFInstance>('FnF', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        settlementId: {
            type: DataTypes.STRING,
            allowNull: true,
            unique: true
        },
        resignationId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'resignations',
                key: 'id'
            }
        },
        terminationRequestId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'termination_requests',
                key: 'id'
            }
        },
        outletId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'outlets',
                key: 'id'
            }
        },
        dealerId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'dealers',
                key: 'id'
            }
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'Initiated'
        },
        totalReceivables: {
            type: DataTypes.DECIMAL(15, 2),
            defaultValue: 0
        },
        totalPayables: {
            type: DataTypes.DECIMAL(15, 2),
            defaultValue: 0
        },
        totalDeductions: {
            type: DataTypes.DECIMAL(15, 2),
            defaultValue: 0
        },
        netAmount: {
            type: DataTypes.DECIMAL(15, 2),
            defaultValue: 0
        },
        settlementAmount: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: true
        },
        settlementDate: {
            type: DataTypes.DATE,
            allowNull: true
        },
        paymentMode: {
            type: DataTypes.STRING,
            allowNull: true
        },
        transactionReference: {
            type: DataTypes.STRING,
            allowNull: true
        },
        remarks: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        clearanceDocuments: {
            type: DataTypes.JSON,
            defaultValue: []
        },
        progressPercentage: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        timeline: {
            type: DataTypes.JSON,
            defaultValue: []
        }
    }, {
        tableName: 'fnf_settlements',
        timestamps: true,
        indexes: [
            { fields: ['resignationId'] },
            { fields: ['terminationRequestId'] },
            { fields: ['outletId'] },
            { fields: ['status'] }
        ]
    });

    (FnF as any).associate = (models: any) => {
        FnF.belongsTo(models.Resignation, { foreignKey: 'resignationId', as: 'resignation' });
        FnF.belongsTo(models.TerminationRequest, { foreignKey: 'terminationRequestId', as: 'terminationRequest' });
        FnF.belongsTo(models.Outlet, { foreignKey: 'outletId', as: 'outlet' });
        FnF.belongsTo(models.Dealer, { foreignKey: 'dealerId', as: 'dealer' });
        FnF.hasMany(models.FnFLineItem, { foreignKey: 'fnfId', as: 'lineItems' });
        FnF.hasMany(models.FffClearance, { foreignKey: 'fnfId', as: 'clearances' });
        
        // Stakeholders/Participants for WorkNote chat logic
        FnF.hasMany(models.RequestParticipant, { 
            foreignKey: 'requestId', 
            constraints: false, 
            scope: { requestType: 'fnf' }, 
            as: 'participants' 
        });
    };

    return FnF;
};
