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

export interface FnFLineItemAttributes {
    id: string;
    fnfId: string;
    itemType: 'Payable' | 'Receivable' | 'Deduction' | 'Recovery';
    description: string;
    department: string;
    amount: number;
    addedBy: string | null;
    sourceType: 'DepartmentClaim' | 'FinanceValidated';
    version: number;
    isActive: boolean;
    parentLineItemId: string | null;
    claimAmount: number | null;
    validatedAmount: number | null;
    varianceAmount: number;
    financeDecision: 'Accepted' | 'Partially Accepted' | 'Rejected' | 'Under Clarification' | null;
    varianceReason: string | null;
}

export interface FnFLineItemInstance extends Model<FnFLineItemAttributes>, FnFLineItemAttributes { }

export default (sequelize: Sequelize) => {
    const FnFLineItem = sequelize.define<FnFLineItemInstance>('FnFLineItem', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        fnfId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'fnf_settlements',
                key: 'id'
            }
        },
        itemType: {
            type: DataTypes.ENUM('Payable', 'Receivable', 'Deduction', 'Recovery'),
            allowNull: false
        },
        description: {
            type: DataTypes.STRING,
            allowNull: false
        },
        department: {
            type: DataTypes.STRING,
            allowNull: false
        },
        amount: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            defaultValue: 0
        },
        addedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        sourceType: {
            type: DataTypes.ENUM('DepartmentClaim', 'FinanceValidated'),
            allowNull: false,
            defaultValue: 'FinanceValidated'
        },
        version: {
            type: DataTypes.INTEGER,
            allowNull: false,
            defaultValue: 1
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: true
        },
        parentLineItemId: {
            type: DataTypes.UUID,
            allowNull: true
        },
        claimAmount: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: true
        },
        validatedAmount: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: true
        },
        varianceAmount: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            defaultValue: 0
        },
        financeDecision: {
            type: DataTypes.ENUM('Accepted', 'Partially Accepted', 'Rejected', 'Under Clarification'),
            allowNull: true
        },
        varianceReason: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'fnf_line_items',
        timestamps: true,
        indexes: [
            { fields: ['fnfId'] },
            { fields: ['itemType'] },
            { fields: ['department'] },
            { fields: ['isActive'] },
            { fields: ['sourceType'] }
        ]
    });

    (FnFLineItem as any).associate = (models: any) => {
        FnFLineItem.belongsTo(models.FnF, {
            foreignKey: 'fnfId',
            as: 'settlement'
        });
        FnFLineItem.belongsTo(models.User, {
            foreignKey: 'addedBy',
            as: 'creator'
        });
    };

    return FnFLineItem;
};
