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

export interface TerminationScnResponseAttributes {
    id: string;
    terminationRequestId: string;
    submittedBy: string; // Dealer User ID
    responseBody: string;
    documents: any[];
    submittedAt: Date;
    status: string; // 'Submitted', 'Reviewed'
}

export interface TerminationScnResponseInstance extends Model<TerminationScnResponseAttributes>, TerminationScnResponseAttributes { }

export default (sequelize: Sequelize) => {
    const TerminationScnResponse = sequelize.define<TerminationScnResponseInstance>('TerminationScnResponse', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        terminationRequestId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'termination_requests',
                key: 'id'
            }
        },
        submittedBy: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        responseBody: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        documents: {
            type: DataTypes.JSON,
            defaultValue: []
        },
        submittedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'Submitted'
        }
    }, {
        tableName: 'termination_scn_responses',
        timestamps: true
    });

    (TerminationScnResponse as any).associate = (models: any) => {
        TerminationScnResponse.belongsTo(models.TerminationRequest, { foreignKey: 'terminationRequestId', as: 'terminationRequest' });
        TerminationScnResponse.belongsTo(models.User, { foreignKey: 'submittedBy', as: 'submitter' });
    };

    return TerminationScnResponse;
};
