const { DataTypes } = require("sequelize");
const sequelize = require("../db");

const Notification = sequelize.define(
  "Notification",
  {
    entityType: {
      type: DataTypes.ENUM(
        "Deviations",
        "Site Monitoring",
        "Case Investigation",
        "SDS",
        "Risk Assessment",
        "Job Safety Analysis",
        "Training",
        "Leave Request",
      ),
      allowNull: false,
      comment: "Type of task",
    },
    entityId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: "ID of the report/SDS that needs action",
    },
    taskNumber: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "Task number (e.g., Task #001)",
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: "Task title/description",
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: "Detailed description of what needs to be done",
    },
    status: {
      type: DataTypes.ENUM("pending", "in-progress", "completed", "cancelled"),
      allowNull: false,
      defaultValue: "pending",
    },
    priority: {
      type: DataTypes.ENUM("low", "medium", "high", "urgent"),
      allowNull: false,
      defaultValue: "medium",
    },
    dueDate: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "When the task should be completed",
    },
    completedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "When the task was completed",
    },
    isRead: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: "Whether the user has seen this notification",
    },
    readAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "When the notification was read",
    },
    metadata: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: "Additional data (severity, location, etc.)",
    },
  },
  {
    tableName: "notifications",
    timestamps: true,
    indexes: [
      {
        fields: ["userId"],
      },
      {
        fields: ["assignedBy"],
      },
      {
        fields: ["companyId"],
      },
      {
        fields: ["projectId"],
      },
      {
        fields: ["entityType"],
      },
      {
        fields: ["status"],
      },
      {
        fields: ["isRead"],
      },
      {
        fields: ["dueDate"],
      },
      {
        fields: ["createdAt"],
      },
    ],
  },
);

module.exports = Notification;
