import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';

export class ContentCatalog extends Model<
  InferAttributes<ContentCatalog>,
  InferCreationAttributes<ContentCatalog>
> {
  declare id: CreationOptional<string>;
  declare content_type: string;
  declare payload: unknown;
  declare active: CreationOptional<boolean>;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;

  static associate(_db: Db): void {

  }
}

export default function (sequelize: Sequelize): typeof ContentCatalog {
  ContentCatalog.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      content_type: {
        type: DataTypes.TEXT,
        allowNull: false,
        unique: true,
      },
      payload: {
        type: DataTypes.JSONB,
        allowNull: false,
      },
      active: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
    },
    {
      sequelize,
      modelName: 'content_catalog',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return ContentCatalog;
}
