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

export interface StateAttributes {
    id: string;
    name: string;
    zoneId?: string | null;
}

export interface StateInstance extends Model<StateAttributes>, StateAttributes { }

export default (sequelize: Sequelize) => {
    const State = sequelize.define<StateInstance>('State', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        },
        zoneId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'zones',
                key: 'id'
            }
        }
    }, {
        tableName: 'states',
        timestamps: true
    });

    (State as any).associate = (models: any) => {
        State.belongsTo(models.Zone, { foreignKey: 'zoneId', as: 'zone' });
        State.hasMany(models.District, { foreignKey: 'stateId', as: 'districts' });
    };

    return State;
};
