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

interface ClaimInvoiceItemAttributes {
    itemId: string;
    requestId: string;
    invoiceNumber?: string;
    transactionCode?: string;
    slNo: number;
    description: string;
    hsnCd: string;
    qty: number;
    unit: string;
    unitPrice: number;
    assAmt: number;
    gstRt: number;
    igstAmt: number;
    cgstAmt: number;
    sgstAmt: number;
    utgstAmt: number;
    totItemVal: number;
    isServc: string;
    igstRate?: number;
    cgstRate?: number;
    sgstRate?: number;
    utgstRate?: number;
    expenseIds?: string[];
    createdAt: Date;
    updatedAt: Date;
}

interface ClaimInvoiceItemCreationAttributes extends Optional<ClaimInvoiceItemAttributes, 'itemId' | 'invoiceNumber' | 'transactionCode' | 'expenseIds' | 'createdAt' | 'updatedAt' | 'utgstAmt' | 'igstRate' | 'cgstRate' | 'sgstRate' | 'utgstRate'> { }

class ClaimInvoiceItem extends Model<ClaimInvoiceItemAttributes, ClaimInvoiceItemCreationAttributes> implements ClaimInvoiceItemAttributes {
    public itemId!: string;
    public requestId!: string;
    public invoiceNumber?: string;
    public transactionCode?: string;
    public slNo!: number;
    public description!: string;
    public hsnCd!: string;
    public qty!: number;
    public unit!: string;
    public unitPrice!: number;
    public assAmt!: number;
    public gstRt!: number;
    public igstAmt!: number;
    public cgstAmt!: number;
    public sgstAmt!: number;
    public utgstAmt!: number;
    public totItemVal!: number;
    public isServc!: string;
    public igstRate?: number;
    public cgstRate?: number;
    public sgstRate?: number;
    public utgstRate?: number;
    public expenseIds?: string[];
    public createdAt!: Date;
    public updatedAt!: Date;
}

ClaimInvoiceItem.init(
    {
        itemId: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
            field: 'item_id',
        },
        requestId: {
            type: DataTypes.UUID,
            allowNull: false,
            field: 'request_id',
            references: {
                model: 'workflow_requests',
                key: 'request_id',
            },
            onDelete: 'CASCADE',
            onUpdate: 'CASCADE',
        },
        invoiceNumber: {
            type: DataTypes.STRING(100),
            allowNull: true,
            field: 'invoice_number',
        },
        transactionCode: {
            type: DataTypes.STRING(100),
            allowNull: true,
            field: 'transaction_code',
        },
        slNo: {
            type: DataTypes.INTEGER,
            allowNull: false,
            field: 'sl_no',
        },
        description: {
            type: DataTypes.TEXT,
            allowNull: false,
            field: 'description',
        },
        hsnCd: {
            type: DataTypes.STRING(20),
            allowNull: false,
            field: 'hsn_cd',
        },
        qty: {
            type: DataTypes.DECIMAL(15, 3),
            allowNull: false,
            field: 'qty',
        },
        unit: {
            type: DataTypes.STRING(20),
            allowNull: false,
            field: 'unit',
        },
        unitPrice: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            field: 'unit_price',
        },
        assAmt: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            field: 'ass_amt',
        },
        gstRt: {
            type: DataTypes.DECIMAL(5, 2),
            allowNull: false,
            field: 'gst_rt',
        },
        igstAmt: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            field: 'igst_amt',
        },
        cgstAmt: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            field: 'cgst_amt',
        },
        sgstAmt: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            field: 'sgst_amt',
        },
        utgstAmt: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: true,
            defaultValue: 0,
            field: 'utgst_amt',
        },
        igstRate: {
            type: DataTypes.DECIMAL(5, 2),
            allowNull: true,
            defaultValue: 0,
            field: 'igst_rate',
        },
        cgstRate: {
            type: DataTypes.DECIMAL(5, 2),
            allowNull: true,
            defaultValue: 0,
            field: 'cgst_rate',
        },
        sgstRate: {
            type: DataTypes.DECIMAL(5, 2),
            allowNull: true,
            defaultValue: 0,
            field: 'sgst_rate',
        },
        utgstRate: {
            type: DataTypes.DECIMAL(5, 2),
            allowNull: true,
            defaultValue: 0,
            field: 'utgst_rate',
        },
        totItemVal: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: false,
            field: 'tot_item_val',
        },
        isServc: {
            type: DataTypes.STRING(1),
            allowNull: false,
            field: 'is_servc',
        },
        expenseIds: {
            type: DataTypes.JSONB,
            allowNull: true,
            field: 'expense_ids',
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
            field: 'created_at',
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
            field: 'updated_at',
        },
    },
    {
        sequelize,
        modelName: 'ClaimInvoiceItem',
        tableName: 'claim_invoice_items',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        indexes: [
            { fields: ['request_id'], name: 'idx_claim_invoice_items_request_id' },
            { fields: ['invoice_number'], name: 'idx_claim_invoice_items_invoice_number' },
        ],
    }
);

WorkflowRequest.hasMany(ClaimInvoiceItem, {
    as: 'invoiceItems',
    foreignKey: 'requestId',
    sourceKey: 'requestId',
});

ClaimInvoiceItem.belongsTo(WorkflowRequest, {
    as: 'workflowRequest',
    foreignKey: 'requestId',
    targetKey: 'requestId',
});

export { ClaimInvoiceItem };
