import { QueryInterface, DataTypes } from 'sequelize';

module.exports = {
    up: async (queryInterface: QueryInterface) => {
        await queryInterface.createTable('claim_invoice_items', {
            item_id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                primaryKey: true,
            },
            request_id: {
                type: DataTypes.UUID,
                allowNull: false,
                references: {
                    model: 'workflow_requests',
                    key: 'request_id',
                },
                onDelete: 'CASCADE',
                onUpdate: 'CASCADE',
            },
            invoice_number: {
                type: DataTypes.STRING(100),
                allowNull: true,
            },
            transaction_code: {
                type: DataTypes.STRING(100),
                allowNull: true,
            },
            sl_no: {
                type: DataTypes.INTEGER,
                allowNull: false,
            },
            description: {
                type: DataTypes.TEXT,
                allowNull: false,
            },
            hsn_cd: {
                type: DataTypes.STRING(20),
                allowNull: false,
            },
            qty: {
                type: DataTypes.DECIMAL(15, 3),
                allowNull: false,
            },
            unit: {
                type: DataTypes.STRING(20),
                allowNull: false,
            },
            unit_price: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: false,
            },
            ass_amt: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: false,
            },
            gst_rt: {
                type: DataTypes.DECIMAL(5, 2),
                allowNull: false,
            },
            igst_amt: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: false,
            },
            cgst_amt: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: false,
            },
            sgst_amt: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: false,
            },
            utgst_amt: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: true,
                defaultValue: 0,
            },
            cgst_rate: {
                type: DataTypes.DECIMAL(5, 2),
                allowNull: true,
                defaultValue: 0,
            },
            sgst_rate: {
                type: DataTypes.DECIMAL(5, 2),
                allowNull: true,
                defaultValue: 0,
            },
            igst_rate: {
                type: DataTypes.DECIMAL(5, 2),
                allowNull: true,
                defaultValue: 0,
            },
            utgst_rate: {
                type: DataTypes.DECIMAL(5, 2),
                allowNull: true,
                defaultValue: 0,
            },
            tot_item_val: {
                type: DataTypes.DECIMAL(15, 2),
                allowNull: false,
            },
            is_servc: {
                type: DataTypes.STRING(1),
                allowNull: false,
            },
            expense_ids: {
                type: DataTypes.JSONB,
                allowNull: true,
            },
            created_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
            },
            updated_at: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
            },
        });

        // Add indexes
        await queryInterface.addIndex('claim_invoice_items', ['request_id'], {
            name: 'idx_claim_invoice_items_request_id',
        });
        await queryInterface.addIndex('claim_invoice_items', ['invoice_number'], {
            name: 'idx_claim_invoice_items_invoice_number',
        });
    },

    down: async (queryInterface: QueryInterface) => {
        await queryInterface.dropTable('claim_invoice_items');
    },
};
