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

export interface UserRoleAttributes {
    id: string;
    userId: string;
    roleId: string;
    districtId: string | null;
    zoneId: string | null;
    regionId: string | null;
    stateId?: string | null;
    managerCode: string | null;
    isPrimary: boolean;
    isActive: boolean;
    effectiveFrom: Date | null;
    effectiveTo: Date | null;
    assignedAt: Date;
    assignedBy: string | null;
}

export interface UserRoleInstance extends Model<UserRoleAttributes>, UserRoleAttributes { }

export default (sequelize: Sequelize) => {
    const UserRole = sequelize.define<UserRoleInstance>('UserRole', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        userId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        roleId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'roles',
                key: 'id'
            }
        },
        districtId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'districts',
                key: 'id'
            }
        },
        zoneId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'zones',
                key: 'id'
            }
        },
        regionId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'regions',
                key: 'id'
            }
        },
        stateId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'states',
                key: 'id'
            }
        },
        managerCode: {
            type: DataTypes.STRING,
            allowNull: true
        },
        isPrimary: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        },
        effectiveFrom: {
            type: DataTypes.DATE,
            allowNull: true
        },
        effectiveTo: {
            type: DataTypes.DATE,
            allowNull: true
        },
        assignedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        },
        assignedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'user_roles',
        timestamps: true
    });

    (UserRole as any).associate = (models: any) => {
        UserRole.belongsTo(models.User, {
            foreignKey: 'userId',
            as: 'user'
        });
        UserRole.belongsTo(models.Role, {
            foreignKey: 'roleId',
            as: 'role'
        });
        UserRole.belongsTo(models.District, {
            foreignKey: 'districtId',
            as: 'district'
        });
        UserRole.belongsTo(models.Zone, {
            foreignKey: 'zoneId',
            as: 'zone'
        });
        UserRole.belongsTo(models.Region, {
            foreignKey: 'regionId',
            as: 'region'
        });
        UserRole.belongsTo(models.State, {
            foreignKey: 'stateId',
            as: 'state'
        });
        UserRole.belongsTo(models.User, {
            foreignKey: 'assignedBy',
            as: 'assigner'
        });
    };

    return UserRole;
};
