import { Model, DataTypes, Sequelize } from 'sequelize';
import { OUTLET_TYPES, OUTLET_STATUS, REGIONS } from '../../../common/config/constants.js';

export interface OutletAttributes {
    id: string;
    code: string;
    name: string;
    type: typeof OUTLET_TYPES[keyof typeof OUTLET_TYPES];
    address: string;
    city: string;
    state: string;
    pincode: string;
    latitude: number | null;
    longitude: number | null;
    status: typeof OUTLET_STATUS[keyof typeof OUTLET_STATUS];
    establishedDate: string;
    dealerId: string;
    districtId: string;
}

export interface OutletInstance extends Model<OutletAttributes>, OutletAttributes { }

export default (sequelize: Sequelize) => {
    const Outlet = sequelize.define<OutletInstance>('Outlet', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        code: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        type: {
            type: DataTypes.ENUM(...Object.values(OUTLET_TYPES)),
            allowNull: false
        },
        address: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        city: {
            type: DataTypes.STRING,
            allowNull: false
        },
        state: {
            type: DataTypes.STRING,
            allowNull: false
        },
        pincode: {
            type: DataTypes.STRING,
            allowNull: false
        },
        latitude: {
            type: DataTypes.DECIMAL(10, 8),
            allowNull: true
        },
        longitude: {
            type: DataTypes.DECIMAL(11, 8),
            allowNull: true
        },
        status: {
            type: DataTypes.ENUM(...Object.values(OUTLET_STATUS)),
            defaultValue: OUTLET_STATUS.ACTIVE
        },
        establishedDate: {
            type: DataTypes.DATEONLY,
            allowNull: false
        },
        dealerId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        districtId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'districts',
                key: 'id'
            }
        }
    }, {
        tableName: 'outlets',
        timestamps: true,
        indexes: [
            { fields: ['code'] },
            { fields: ['dealerId'] },
            { fields: ['type'] },
            { fields: ['status'] },
            { fields: ['districtId'] }
        ]
    });

    (Outlet as any).associate = (models: any) => {
        Outlet.belongsTo(models.User, {
            foreignKey: 'dealerId',
            as: 'dealer'
        });
        Outlet.belongsTo(models.District, {
            foreignKey: 'districtId',
            as: 'district'
        });
        Outlet.hasMany(models.Resignation, {
            foreignKey: 'outletId',
            as: 'resignations'
        });
        Outlet.hasMany(models.ConstitutionalChange, {
            foreignKey: 'outletId',
            as: 'constitutionalChanges'
        });
        Outlet.hasMany(models.RelocationRequest, {
            foreignKey: 'outletId',
            as: 'relocationRequests'
        });
    };

    return Outlet;
};
