import { User_Model } from "@/contexts/users";
import {
  CreationOptional,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  Model,
  NonAttribute,
  Op,
  Sequelize,
} from "sequelize";
import { Quote_Model } from "./quote.model";

export type DealerCreationAttributes = InferCreationAttributes<
  Dealer_Model,
  { omit: "user" | "quotes" }
> & {
  user_id: string;
  //quotes?: QuoteCreationAttributes[];
};

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

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  static associate(connection: Sequelize) {
    const { Dealer_Model, User_Model } = connection.models;

    Dealer_Model.belongsTo(User_Model, {
      as: "user",
      foreignKey: "user_id",
      onDelete: "RESTRICT",
    });

    Dealer_Model.hasMany(Quote_Model, {
      as: "quotes",
      foreignKey: "dealer_id",
      onDelete: "RESTRICT",
    });
  }

  declare id: string;
  declare id_contact?: CreationOptional<string | null>;
  declare name: CreationOptional<string>;
  declare contact_information: CreationOptional<string>;
  declare default_payment_method: CreationOptional<string>;
  declare default_notes: CreationOptional<string>;
  declare default_legal_terms: CreationOptional<string>;
  declare default_quote_validity: CreationOptional<string>;
  declare default_tax: CreationOptional<number | null>;
  declare status: CreationOptional<string>;
  declare lang_code: CreationOptional<string>;
  declare currency_code: CreationOptional<string>;
  declare logo: CreationOptional<string>;

  declare user: NonAttribute<User_Model>;
  declare quotes: NonAttribute<Quote_Model>;
}

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

      id_contact: {
        type: DataTypes.BIGINT().UNSIGNED,
        allowNull: true,
      },

      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },

      contact_information: DataTypes.TEXT,
      default_payment_method: DataTypes.TEXT,
      default_notes: DataTypes.TEXT,
      default_legal_terms: DataTypes.TEXT,
      default_quote_validity: DataTypes.TEXT,

      default_tax: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: 2100,
      },

      logo: {
        type: DataTypes.STRING,
        allowNull: true,
      },

      lang_code: {
        type: DataTypes.STRING(2),
        allowNull: false,
        defaultValue: "es",
      },

      currency_code: {
        type: new DataTypes.STRING(3),
        allowNull: false,
        defaultValue: "EUR",
      },

      status: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    },
    {
      sequelize,
      tableName: "dealers",

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

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

      indexes: [
        { name: "id_contact_idx", fields: ["id_contact"], unique: true },
        { name: "status_idx", fields: ["status"] },
      ],

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

      defaultScope: {
        attributes: {
          exclude: ["id_contact"],
        },
      },

      scopes: {
        quickSearch(value) {
          return {
            where: {
              [Op.or]: {
                name: {
                  [Op.like]: `%${value}%`,
                },
              },
            },
          };
        },
      },
    }
  );

  return Dealer_Model;
};
