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

export class ActionData extends Model {
  public id!: string;
  public actionType!: string;
  public taskId!: string;
  public dataSource!: string;
  public data!: string;
  public priority!: string;
  public weight!: number;
  public expiration!: number;
  public timestamp!: Date;
  public expiresAt!: Date;
  public metadata!: string;
  public createdAt!: Date;
  public updatedAt!: Date;
}

ActionData.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    actionType: {
      type: DataTypes.STRING,
      allowNull: false,
      index: true,
    },
    taskId: {
      type: DataTypes.UUID,
      allowNull: false,
      index: true,
    },
    dataSource: {
      type: DataTypes.STRING,
      allowNull: false,
      index: true,
    },
    data: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    priority: {
      type: DataTypes.ENUM('critical', 'high', 'medium', 'low'),
      allowNull: false,
      defaultValue: 'medium',
    },
    weight: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
    },
    expiration: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 3600,
    },
    timestamp: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    expiresAt: {
      type: DataTypes.DATE,
      allowNull: false,
      index: true,
    },
    metadata: {
      type: DataTypes.TEXT,
      allowNull: false,
      defaultValue: '{}',
    },
  },
  {
    sequelize,
    tableName: 'action_data',
    timestamps: true,
    indexes: [
      {
        fields: ['actionType', 'taskId'],
        unique: false,
      },
      {
        fields: ['dataSource', 'priority'],
      },
      {
        fields: ['expiresAt'],
      },
    ],
  }
);

export default ActionData;
