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

export type CustomerCreationAttributes = InferCreationAttributes<CustomerModel, {}> & {};

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

  declare id: string;
  declare company_id: string;
  declare reference: CreationOptional<string | null>;

  declare is_company: boolean;
  declare name: string;
  declare trade_name: CreationOptional<string | null>;
  declare tin: CreationOptional<string | null>;

  declare street: CreationOptional<string | null>;
  declare street2: CreationOptional<string | null>;
  declare city: CreationOptional<string | null>;
  declare province: CreationOptional<string | null>;
  declare postal_code: CreationOptional<string | null>;
  declare country: CreationOptional<string | null>;

  // Correos electrónicos
  declare email_primary: CreationOptional<string | null>;
  declare email_secondary: CreationOptional<string | null>;

  // Teléfonos fijos
  declare phone_primary: CreationOptional<string | null>;
  declare phone_secondary: CreationOptional<string | null>;

  // Móviles
  declare mobile_primary: CreationOptional<string | null>;
  declare mobile_secondary: CreationOptional<string | null>;

  declare fax: CreationOptional<string | null>;
  declare website: CreationOptional<string | null>;

  declare legal_record: CreationOptional<string | null>;

  declare default_taxes: CreationOptional<string | null>;
  declare status: string;
  declare language_code: CreationOptional<string>;
  declare currency_code: CreationOptional<string>;

  declare factuges_id: CreationOptional<string | null>;

  static associate(database: Sequelize) {}

  static hooks(database: Sequelize) {}
}

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

      street: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      street2: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      city: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      province: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      postal_code: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      country: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },

      email_primary: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
        validate: {
          isEmail: true,
        },
      },
      email_secondary: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
        validate: {
          isEmail: true,
        },
      },

      phone_primary: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      phone_secondary: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },

      mobile_primary: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
      mobile_secondary: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },

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

      default_taxes: {
        type: DataTypes.STRING,
        defaultValue: null,
        allowNull: true,
      },

      status: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: "active",
      },

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

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

      factuges_id: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
    },
    {
      sequelize: database,
      tableName: "customers",

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

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

      indexes: [
        { name: "company_idx", fields: ["company_id"], unique: false },
        { name: "idx_company_idx", fields: ["id", "company_id"], unique: true },
        {
          name: "ft_customer",
          type: "FULLTEXT",
          fields: ["name", "trade_name", "reference", "tin", "email_primary", "mobile_primary"],
        },
      ],

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

      defaultScope: {},

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