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

interface ActivityTypeAttributes {
  activityTypeId: string;
  title: string;
  itemCode?: string;
  taxationType?: string;
  sapRefNo?: string;
  isActive: boolean;
  creditPostingOn?: string | null;
  createdBy: string;
  updatedBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface ActivityTypeCreationAttributes extends Optional<ActivityTypeAttributes, 'activityTypeId' | 'itemCode' | 'taxationType' | 'sapRefNo' | 'isActive' | 'creditPostingOn' | 'updatedBy' | 'createdAt' | 'updatedAt'> { }

class ActivityType extends Model<ActivityTypeAttributes, ActivityTypeCreationAttributes> implements ActivityTypeAttributes {
  public activityTypeId!: string;
  public title!: string;
  public itemCode?: string;
  public taxationType?: string;
  public sapRefNo?: string;
  public isActive!: boolean;
  public creditPostingOn?: string | null;
  public createdBy!: string;
  public updatedBy?: string;
  public createdAt!: Date;
  public updatedAt!: Date;

  // Associations
  public creator?: User;
  public updater?: User;
}

ActivityType.init(
  {
    activityTypeId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'activity_type_id'
    },
    title: {
      type: DataTypes.STRING(200),
      allowNull: false,
      unique: true,
      field: 'title'
    },
    itemCode: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: null,
      field: 'item_code'
    },
    taxationType: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: null,
      field: 'taxation_type'
    },
    sapRefNo: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: null,
      field: 'sap_ref_no'
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'is_active'
    },
    creditPostingOn: {
      type: DataTypes.STRING(50),
      allowNull: true,
      defaultValue: null,
      field: 'credit_posting_on',
      comment: 'Indicates what the credit note is posted against (e.g. "Spares", "Vehicle")'
    },
    createdBy: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'created_by'
    },
    updatedBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'updated_by'
    },
    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,
    modelName: 'ActivityType',
    tableName: 'activity_types',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['title'], unique: true },
      { fields: ['is_active'] },
      { fields: ['item_code'] },
      { fields: ['created_by'] }
    ]
  }
);

// Associations
ActivityType.belongsTo(User, {
  as: 'creator',
  foreignKey: 'createdBy',
  targetKey: 'userId'
});

ActivityType.belongsTo(User, {
  as: 'updater',
  foreignKey: 'updatedBy',
  targetKey: 'userId'
});

export { ActivityType };

