import { Model, DataTypes, Sequelize } from 'sequelize';
import { RELOCATION_TYPES, RELOCATION_STAGES } from '../../../../common/config/constants.js';

export interface RelocationRequestAttributes {
    id: string;
    requestId: string;
    outletId: string;
    dealerId: string;
    relocationType: typeof RELOCATION_TYPES[keyof typeof RELOCATION_TYPES];
    newAddress: string;
    newCity: string;
    newState: string;
    newDistrictId: string | null;
    newStateId: string | null;
    reason: string;
    currentStage: typeof RELOCATION_STAGES[keyof typeof RELOCATION_STAGES];
    status: string;
    progressPercentage: number;
    documents: any[];
    timeline: any[];
    distance: string | null;
    propertyType: string | null;
    expectedRelocationDate: Date | null;
    currentLatitude: number | null;
    currentLongitude: number | null;
    newLatitude: number | null;
    newLongitude: number | null;
}

export interface RelocationRequestInstance extends Model<RelocationRequestAttributes>, RelocationRequestAttributes { }

export default (sequelize: Sequelize) => {
    const RelocationRequest = sequelize.define<RelocationRequestInstance>('RelocationRequest', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false
        },
        outletId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'outlets',
                key: 'id'
            }
        },
        dealerId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        relocationType: {
            type: DataTypes.ENUM(...Object.values(RELOCATION_TYPES)),
            allowNull: false
        },
        newAddress: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        newCity: {
            type: DataTypes.STRING,
            allowNull: false
        },
        newState: {
            type: DataTypes.STRING,
            allowNull: false
        },
        newDistrictId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'districts',
                key: 'id'
            }
        },
        newStateId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'states',
                key: 'id'
            }
        },
        reason: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        currentStage: {
            type: DataTypes.ENUM(...Object.values(RELOCATION_STAGES)),
            defaultValue: RELOCATION_STAGES.ASM_REVIEW
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'Pending'
        },
        progressPercentage: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        documents: {
            type: DataTypes.JSON,
            defaultValue: []
        },
        timeline: {
            type: DataTypes.JSON,
            defaultValue: []
        },
        distance: {
            type: DataTypes.STRING,
            allowNull: true
        },
        propertyType: {
            type: DataTypes.STRING,
            allowNull: true
        },
        expectedRelocationDate: {
            type: DataTypes.DATEONLY,
            allowNull: true
        },
        currentLatitude: {
            type: DataTypes.DECIMAL(10, 8),
            allowNull: true
        },
        currentLongitude: {
            type: DataTypes.DECIMAL(11, 8),
            allowNull: true
        },
        newLatitude: {
            type: DataTypes.DECIMAL(10, 8),
            allowNull: true
        },
        newLongitude: {
            type: DataTypes.DECIMAL(11, 8),
            allowNull: true
        }
    }, {
        tableName: 'relocation_requests',
        timestamps: true,
        indexes: [
            { fields: ['requestId'] },
            { fields: ['outletId'] },
            { fields: ['dealerId'] },
            { fields: ['currentStage'] }
        ]
    });

    (RelocationRequest as any).associate = (models: any) => {
        RelocationRequest.belongsTo(models.Outlet, {
            foreignKey: 'outletId',
            as: 'outlet'
        });
        RelocationRequest.belongsTo(models.User, {
            foreignKey: 'dealerId',
            as: 'dealer'
        });
        RelocationRequest.belongsTo(models.District, {
            foreignKey: 'newDistrictId',
            as: 'newDistrict'
        });
        RelocationRequest.belongsTo(models.State, {
            foreignKey: 'newStateId',
            as: 'newStateRef'
        });
        RelocationRequest.hasMany(models.Worknote, {
            foreignKey: 'requestId',
            as: 'worknotes',
            scope: { requestType: 'relocation' },
            constraints: false
        });
        RelocationRequest.hasMany(models.RelocationDocument, {
            foreignKey: 'relocationId',
            as: 'uploadedDocuments'
        });
        RelocationRequest.hasOne(models.EorChecklist, {
            foreignKey: 'relocationId',
            as: 'eorChecklist'
        });
        RelocationRequest.hasMany(models.RequestParticipant, {
            foreignKey: 'requestId',
            as: 'participants',
            scope: { requestType: 'relocation' },
            constraints: false
        });
        // Note: Participants are computed dynamically based on outlet location hierarchy
        // See getRequestById in relocation.controller.ts
    };

    return RelocationRequest;
};
