import {
  CreationOptional,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  Model,
  NonAttribute,
  Op,
  Sequelize,
} from "sequelize";
import { ArticleTranslation_Model } from "./articleTraslation.model.ts.bak";

export type ArticleCreationAttributes = InferCreationAttributes<
  Article_Model,
  {
    omit: "translations";
  }
>;

export class Article_Model extends Model<
  InferAttributes<Article_Model, { omit: "translations" }>,
  InferCreationAttributes<Article_Model, { omit: "translations" }>
> {
  // To avoid table creation
  static async sync(): Promise<any> {
    return Promise.resolve();
  }

  static associate(connection: Sequelize) {
    const { Article_Model, ArticleTranslation_Model } = connection.models;

    Article_Model.hasMany(ArticleTranslation_Model, {
      as: "translations",
      foreignKey: "catalog_id",
      onDelete: "CASCADE",
    });
  }

  declare id: string;

  declare catalog_name: string;
  declare id_article: string; // number ??
  declare reference: CreationOptional<string>;
  //declare description: CreationOptional<string>;
  declare points: CreationOptional<number>;
  declare retail_price: CreationOptional<number>;

  declare translations: NonAttribute<ArticleTranslation_Model[]>;
}

export default (sequelize: Sequelize) => {
  Article_Model.init(
    {
      id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },

      catalog_name: {
        type: DataTypes.STRING(),
        allowNull: false,
      },

      id_article: {
        type: DataTypes.BIGINT().UNSIGNED,
        allowNull: false,
      },

      reference: DataTypes.STRING(),
      //description: DataTypes.STRING(),
      points: {
        type: DataTypes.BIGINT().UNSIGNED,
        defaultValue: 0,
      },

      retail_price: DataTypes.BIGINT(),
    },
    {
      sequelize,
      tableName: "catalog",

      paranoid: true, // softs deletes
      timestamps: true,
      //version: true,

      createdAt: "created_at",
      updatedAt: "updated_at",
      deletedAt: "deleted_at",

      indexes: [
        { name: "catalog_name_idx", fields: ["catalog_name"] },
        { name: "id_article_idx", fields: ["id_article"] },
        { name: "updated_at_idx", fields: ["updated_at"] },
      ],

      whereMergeStrategy: "and", // <- cómo tratar el merge de un scope

      defaultScope: {
        attributes: { exclude: ["id_article", "reference"] },
        include: [
          {
            attributes: ["description"],
            model: ArticleTranslation_Model,
            as: "translations",
            required: true,
            where: {
              lang_code: "es",
            },
          },
        ],
      },

      scopes: {
        quickSearch(value) {
          return {
            include: [
              {
                model: ArticleTranslation_Model,
                as: "translations",
                required: true,
                where: {
                  [Op.or]: {
                    reference: {
                      [Op.like]: `%${value}%`,
                    },
                    description: {
                      [Op.like]: `%${value}%`,
                    },
                  },
                },
              },
            ],
          };
        },
      },
    }
  );

  return Article_Model;
};
