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

export interface DistrictAttributes {
    id: string;
    name: string;
    code?: string;
    stateId?: string | null;
    regionId?: string | null;
    zoneId?: string | null;
    asmId?: string | null;
    asmCode?: string | null;
    ddAmId?: string | null;
    ddAmCode?: string | null;
    zmId?: string | null;
    zmCode?: string | null;
    city?: string | null;
    isOpportunity?: boolean;
    description?: string | null;
}

export interface DistrictInstance extends Model<DistrictAttributes>, DistrictAttributes { }

export default (sequelize: Sequelize) => {
    const District = sequelize.define<DistrictInstance>('District', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        code: {
            type: DataTypes.STRING,
            allowNull: true,
            unique: true
        },
        stateId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'states',
                key: 'id'
            }
        },
        regionId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'regions',
                key: 'id'
            }
        },
        zoneId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'zones',
                key: 'id'
            }
        },
        asmId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: { model: 'users', key: 'id' }
        },
        asmCode: {
            type: DataTypes.STRING,
            allowNull: true
        },
        ddAmId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: { model: 'users', key: 'id' }
        },
        ddAmCode: {
            type: DataTypes.STRING,
            allowNull: true
        },
        zmId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: { model: 'users', key: 'id' }
        },
        zmCode: {
            type: DataTypes.STRING,
            allowNull: true
        },
        city: {
            type: DataTypes.STRING,
            allowNull: true
        },
        isOpportunity: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        },
        description: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'districts',
        timestamps: true,
        indexes: [
            { fields: ['stateId'] },
            { fields: ['regionId'] },
            { fields: ['zoneId'] },
            { unique: true, fields: ['name', 'stateId'] }
        ]
    });

    (District as any).associate = (models: any) => {
        District.belongsTo(models.State, { foreignKey: 'stateId', as: 'state' });
        District.belongsTo(models.Region, { foreignKey: 'regionId', as: 'region' });
        District.belongsTo(models.Zone, { foreignKey: 'zoneId', as: 'zone' });
        District.belongsTo(models.User, { foreignKey: 'asmId', as: 'asm' });
        District.belongsTo(models.User, { foreignKey: 'ddAmId', as: 'ddAm' });
        District.belongsTo(models.User, { foreignKey: 'zmId', as: 'zonalManager' });
        District.hasMany(models.User, { foreignKey: 'districtId', as: 'users' });
        District.hasMany(models.UserRole, { foreignKey: 'districtId', as: 'userRoles' });
        District.hasMany(models.Application, { foreignKey: 'districtId', as: 'applications' });
        District.hasMany(models.Opportunity, { foreignKey: 'districtId', as: 'opportunities' });
        District.hasMany(models.Location, { foreignKey: 'districtId', as: 'locations' }); // Will be added soon
    };

    return District;
};
