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

export interface DocumentAttributes {
    id: string;
    applicationId: string | null;
    dealerId: string | null;
    documentType: string;
    fileName: string;
    filePath: string;
    fileSize: number | null;
    mimeType: string | null;
    stage: string | null;
    status: string;
    uploadedBy: string | null;
}

export interface OnboardingDocumentInstance extends Model<DocumentAttributes>, DocumentAttributes { }

export default (sequelize: Sequelize) => {
    const OnboardingDocument = sequelize.define<OnboardingDocumentInstance>('OnboardingDocument', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        dealerId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'dealers',
                key: 'id'
            }
        },
        documentType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        fileName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        filePath: {
            type: DataTypes.STRING,
            allowNull: false
        },
        fileSize: {
            type: DataTypes.INTEGER,
            allowNull: true
        },
        mimeType: {
            type: DataTypes.STRING,
            allowNull: true
        },
        stage: {
            type: DataTypes.STRING,
            allowNull: true
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'active'
        },
        uploadedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'onboarding_documents',
        timestamps: true,
        indexes: [
            { fields: ['applicationId'] },
            { fields: ['dealerId'] },
            { fields: ['applicationId', 'stage'] },
            { fields: ['documentType'] }
        ]
    });

    (OnboardingDocument as any).associate = (models: any) => {
        OnboardingDocument.belongsTo(models.User, { foreignKey: 'uploadedBy', as: 'uploader' });
        OnboardingDocument.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
        OnboardingDocument.belongsTo(models.Dealer, { foreignKey: 'dealerId', as: 'dealer' });

        OnboardingDocument.hasMany(models.DocumentVersion, { foreignKey: 'documentId', as: 'versions', constraints: false });
        OnboardingDocument.belongsToMany(models.Worknote, {
            through: models.WorkNoteAttachment,
            foreignKey: 'documentId',
            otherKey: 'noteId',
            as: 'workNotes',
            constraints: false
        });
    };

    return OnboardingDocument;
};
