import {
  CreationOptional,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  Model,
  Sequelize,
} from "sequelize";

export type ContactCreationAttributes = InferCreationAttributes<ContactModel, {}> & {};

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

  declare id: string;
  declare reference: CreationOptional<string>;

  declare is_companyr: boolean;
  declare name: string;
  declare trade_name: CreationOptional<string>;
  declare tin: string;

  declare street: string;
  declare city: string;
  declare state: string;
  declare postal_code: string;
  declare country: string;

  declare email: string;
  declare phone: string;
  declare fax: CreationOptional<string>;
  declare website: CreationOptional<string>;

  declare legal_record: string;

  declare default_tax: number;
  declare status: string;
  declare language_code: string;
  declare currency_code: string;
}

export default (sequelize: Sequelize) => {
  ContactModel.init(
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
      },
      reference: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      is_companyr: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      trade_name: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      tin: {
        type: DataTypes.STRING,
        allowNull: false,
      },

      street: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      city: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      state: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      postal_code: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      country: {
        type: DataTypes.STRING,
        allowNull: false,
      },

      email: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          isEmail: true,
        },
      },
      phone: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      fax: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      website: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
        validate: {
          isUrl: true,
        },
      },
      legal_record: {
        type: DataTypes.TEXT,
        allowNull: false,
      },

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

      language_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,
        defaultValue: "active",
      },
    },
    {
      sequelize,
      tableName: "contacts",

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

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

      indexes: [
        { name: "email_idx", fields: ["email"], unique: true },
        { name: "reference_idx", fields: ["reference"], unique: true },
      ],

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

      defaultScope: {},

      scopes: {},
    }
  );
  return ContactModel;
};
