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

export interface FddReportAttributes {
    id: string;
    assignmentId: string;
    reportDocumentId: string | null;
    findings: string | null;
    recommendation: string | null;
    verifiedAt: Date | null;
    verifiedBy: string | null;
    submittedBy: string | null;
}

export interface FddReportInstance extends Model<FddReportAttributes>, FddReportAttributes { }

export default (sequelize: Sequelize) => {
    const FddReport = sequelize.define<FddReportInstance>('FddReport', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        assignmentId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'fdd_assignments',
                key: 'id'
            }
        },
        reportDocumentId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'onboarding_documents',
                key: 'id'
            }
        },
        findings: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        recommendation: {
            type: DataTypes.STRING,
            allowNull: true
        },
        verifiedAt: {
            type: DataTypes.DATE,
            allowNull: true
        },
        verifiedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        submittedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'fdd_reports',
        timestamps: true
    });

    (FddReport as any).associate = (models: any) => {
        FddReport.belongsTo(models.FddAssignment, { foreignKey: 'assignmentId', as: 'assignment' });
        FddReport.belongsTo(models.OnboardingDocument, { foreignKey: 'reportDocumentId', as: 'reportDocument' });
        FddReport.belongsTo(models.User, { foreignKey: 'verifiedBy', as: 'verifier' });
        FddReport.belongsTo(models.User, { foreignKey: 'submittedBy', as: 'submitter' });
    };

    return FddReport;
};
