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

interface RequestSummaryAttributes {
  summaryId: string;
  requestId: string;
  initiatorId: string;
  title: string;
  description: string | null;
  closingRemarks: string | null;
  isAiGenerated: boolean;
  conclusionId: string | null;
  createdAt?: Date;
  updatedAt?: Date;
}

interface RequestSummaryCreationAttributes
  extends Optional<RequestSummaryAttributes, 'summaryId' | 'description' | 'closingRemarks' | 'isAiGenerated' | 'conclusionId' | 'createdAt' | 'updatedAt'> {}

class RequestSummary extends Model<RequestSummaryAttributes, RequestSummaryCreationAttributes>
  implements RequestSummaryAttributes {
  public summaryId!: string;
  public requestId!: string;
  public initiatorId!: string;
  public title!: string;
  public description!: string | null;
  public closingRemarks!: string | null;
  public isAiGenerated!: boolean;
  public conclusionId!: string | null;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // Associations
  public request?: WorkflowRequest;
  public initiator?: User;
  public conclusion?: ConclusionRemark;
}

RequestSummary.init(
  {
    summaryId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'summary_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      },
      unique: true
    },
    initiatorId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'initiator_id',
      references: {
        model: 'users',
        key: 'user_id'
      }
    },
    title: {
      type: DataTypes.STRING(500),
      allowNull: false
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    closingRemarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'closing_remarks'
    },
    isAiGenerated: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      field: 'is_ai_generated'
    },
    conclusionId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'conclusion_id',
      references: {
        model: 'conclusion_remarks',
        key: 'conclusion_id'
      }
    },
    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: 'request_summaries',
    timestamps: true,
    underscored: true
  }
);

// Associations
RequestSummary.belongsTo(WorkflowRequest, {
  as: 'request',
  foreignKey: 'requestId',
  targetKey: 'requestId'
});

RequestSummary.belongsTo(User, {
  as: 'initiator',
  foreignKey: 'initiatorId',
  targetKey: 'userId'
});

RequestSummary.belongsTo(ConclusionRemark, {
  foreignKey: 'conclusionId',
  targetKey: 'conclusionId'
});

export default RequestSummary;

