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

export interface DealerAttributes {
    id: string;
    applicationId: string | null;
    dealerCodeId: string | null;
    legalName: string;
    businessName: string;
    constitutionType: string;
    registeredAddress: string | null;
    gstNumber: string | null;
    panNumber: string | null;
    status: string;
    onboardedAt: Date | null;
    loiDate: Date | null;
    loaDate: Date | null;
    isLegacy: boolean;
    securityDepositAmount: number | null;
    securityDepositDate: Date | null;
    lastWorkingDay: Date | null;
    exitReason: string | null;
    asmId: string | null;
}

export interface DealerInstance extends Model<DealerAttributes>, DealerAttributes { }

export default (sequelize: Sequelize) => {
    const Dealer = sequelize.define<DealerInstance>('Dealer', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        dealerCodeId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'dealer_codes',
                key: 'id'
            }
        },
        legalName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        businessName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        constitutionType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        registeredAddress: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        gstNumber: {
            type: DataTypes.STRING,
            allowNull: true
        },
        panNumber: {
            type: DataTypes.STRING,
            allowNull: true
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'active'
        },
        onboardedAt: {
            type: DataTypes.DATE,
            allowNull: true
        },
        loiDate: {
            type: DataTypes.DATE,
            allowNull: true
        },
        loaDate: {
            type: DataTypes.DATE,
            allowNull: true
        },
        isLegacy: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        securityDepositAmount: {
            type: DataTypes.DECIMAL(15, 2),
            allowNull: true
        },
        securityDepositDate: {
            type: DataTypes.DATE,
            allowNull: true
        },
        lastWorkingDay: {
            type: DataTypes.DATE,
            allowNull: true
        },
        exitReason: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        asmId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'dealers',
        timestamps: true
    });

    (Dealer as any).associate = (models: any) => {
        const { Application, DealerCode, OnboardingDocument, Resignation, TerminationRequest, User } = models;
        
        Dealer.belongsTo(Application, { foreignKey: 'applicationId', as: 'application' });
        Dealer.belongsTo(DealerCode, { foreignKey: 'dealerCodeId', as: 'dealerCode' });

        if (OnboardingDocument) {
            Dealer.hasMany(OnboardingDocument, { foreignKey: 'dealerId', as: 'onboardingDocuments' });
        }
        
        if (Resignation) {
            Dealer.hasMany(Resignation, { foreignKey: 'dealerId', as: 'resignations' });
        }
        
        if (TerminationRequest) {
            Dealer.hasMany(TerminationRequest, { foreignKey: 'dealerId', as: 'terminationRequests' });
        }
        
        if (User) {
            Dealer.hasOne(User, { foreignKey: 'dealerId', as: 'user' });
            Dealer.belongsTo(User, { foreignKey: 'asmId', as: 'asmManager' });
        }
        
        Dealer.hasMany(models.DealerBankDetail, { foreignKey: 'dealerId', as: 'bankDetails' });
    };

    return Dealer;
};
