import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '../config/database';

interface ConclusionRemarkAttributes {
  conclusionId: string;
  requestId: string;
  aiGeneratedRemark: string | null;
  aiModelUsed: string | null;
  aiConfidenceScore: number | null;
  finalRemark: string | null;
  editedBy: string | null;
  isEdited: boolean;
  editCount: number;
  approvalSummary: any;
  documentSummary: any;
  keyDiscussionPoints: string[];
  generatedAt: Date | null;
  finalizedAt: Date | null;
  createdAt?: Date;
  updatedAt?: Date;
}

interface ConclusionRemarkCreationAttributes 
  extends Optional<ConclusionRemarkAttributes, 'conclusionId' | 'aiGeneratedRemark' | 'aiModelUsed' | 'aiConfidenceScore' | 'finalRemark' | 'editedBy' | 'isEdited' | 'editCount' | 'approvalSummary' | 'documentSummary' | 'keyDiscussionPoints' | 'generatedAt' | 'finalizedAt'> {}

class ConclusionRemark extends Model<ConclusionRemarkAttributes, ConclusionRemarkCreationAttributes> 
  implements ConclusionRemarkAttributes {
  public conclusionId!: string;
  public requestId!: string;
  public aiGeneratedRemark!: string | null;
  public aiModelUsed!: string | null;
  public aiConfidenceScore!: number | null;
  public finalRemark!: string | null;
  public editedBy!: string | null;
  public isEdited!: boolean;
  public editCount!: number;
  public approvalSummary!: any;
  public documentSummary!: any;
  public keyDiscussionPoints!: string[];
  public generatedAt!: Date | null;
  public finalizedAt!: Date | null;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

ConclusionRemark.init(
  {
    conclusionId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'conclusion_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    aiGeneratedRemark: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'ai_generated_remark'
    },
    aiModelUsed: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'ai_model_used'
    },
    aiConfidenceScore: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'ai_confidence_score'
    },
    finalRemark: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'final_remark'
    },
    editedBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'edited_by',
      references: {
        model: 'users',
        key: 'user_id'
      }
    },
    isEdited: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'is_edited'
    },
    editCount: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      field: 'edit_count'
    },
    approvalSummary: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'approval_summary'
    },
    documentSummary: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'document_summary'
    },
    keyDiscussionPoints: {
      type: DataTypes.ARRAY(DataTypes.TEXT),
      allowNull: false,
      defaultValue: [],
      field: 'key_discussion_points'
    },
    generatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'generated_at'
    },
    finalizedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'finalized_at'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'updated_at'
    }
  },
  {
    sequelize,
    tableName: 'conclusion_remarks',
    timestamps: true,
    underscored: true
  }
);

export default ConclusionRemark;

