const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const Client = sequelize.define(
    "client",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      code: { type: DataTypes.STRING(255), allowNull: true },
      title: { type: DataTypes.STRING(100), allowNull: true },
      priority: {
        type: DataTypes.ENUM("A", "B", "C", "D"),
        allowNull: true,
      },
      doctor_type: {
        type: DataTypes.ENUM("PROSPECT", "EXIST"),
        allowNull: true,
      },
      saluation: { type: DataTypes.STRING(100), allowNull: true },
      address: { type: DataTypes.TEXT, allowNull: true },
      email: { type: DataTypes.TEXT, allowNull: true },
      mobile_no: { type: DataTypes.STRING(100), allowNull: true },
      birth_date: { type: DataTypes.DATEONLY, allowNull: true },
      birth_place: { type: DataTypes.STRING(100), allowNull: true },
      marital_status: { type: DataTypes.STRING(100), allowNull: true },
      gender: { type: DataTypes.STRING(100), allowNull: true },
      religon: { type: DataTypes.STRING(100), allowNull: true },
      hobbies: { type: DataTypes.STRING(100), allowNull: true },
      bank: { type: DataTypes.STRING(100), allowNull: true },
      bank_no: { type: DataTypes.STRING(100), allowNull: true },
      bank_name: { type: DataTypes.STRING(100), allowNull: true },
      start_balance: { type: DataTypes.DECIMAL(18, 2), allowNull: true },
      credit_balance: { type: DataTypes.DECIMAL(18, 2), allowNull: true },
      debit_balance: { type: DataTypes.DECIMAL(18, 2), allowNull: true },
      final_balance: { type: DataTypes.DECIMAL(18, 2), allowNull: true },

      mother_name: { type: DataTypes.STRING(225), allowNull: true },
      father_name: { type: DataTypes.STRING(225), allowNull: true },

      // fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "companies",
          key: "id",
        },
      },
      clienttype_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "clienttypes",
          key: "id",
        },
      },
      specialist_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "specialists",
          key: "id",
        },
      },
      subspecialist_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "subspecialists",
          key: "id",
        },
      },
      clientsubtype_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "clientsubtypes",
          key: "id",
        },
      },
      province_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "provinces",
          key: "id",
        },
      },
      city_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "cities",
          key: "id",
        },
      },

      // sappsv1 or erpnext
      ids: { type: DataTypes.INTEGER, allowNull: true },
      import_from: { type: DataTypes.STRING(100), allowNull: true },
    },
    {
      tableName: "clients",
    },
    {
      freezeTableName: true,
    },
  );

  /**
   * Define associations for the Client model.
   * @param {object} models - The models to associate with.
   */
  Client.associate = function (models) {
    /**
     * Associates the Client model with other models.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

    /**
     * A Client belongs to a ClientType.
     * Defines a many-to-one relationship between Client and ClientType.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.ClientType, { foreignKey: "clienttype_id", as: "clienttype" });

    /**
     * A Client belongs to a Specialist.
     * Defines a many-to-one relationship between Client and Specialist.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.Specialist, { foreignKey: "specialist_id", as: "specialist" });

    /**
     * A Client belongs to a SubSpecialist.
     * Defines a many-to-one relationship between Client and SubSpecialist.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.SubSpecialist, { foreignKey: "subspecialist_id", as: "subspecialist" });

    /**
     * A Client belongs to a ClientSubtype.
     * Defines a many-to-one relationship between Client and ClientSubtype.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.ClientSubtype, { foreignKey: "clientsubtype_id", as: "clientsubtype" });

    /**
     * A Client belongs to a Province.
     * Defines a many-to-one relationship between Client and Province.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.Province, { foreignKey: "province_id", as: "province" });

    /**
     * A Client belongs to a City.
     * Defines a many-to-one relationship between Client and City.
     * @param {object} models - The models to associate with.
     */
    Client.belongsTo(models.City, { foreignKey: "city_id", as: "city" });
    /**
     * A Client has many OutletClients.
     * Defines a one-to-many relationship between Client and OutletClient.
     * @param {object} models - The models to associate with.
     */
    Client.hasMany(models.OutletClient, { foreignKey: "client_id", as: "outletclients" });

    /**
     * A Client has many CallPlanDestinations.
     * Defines a one-to-many relationship between Client and CallPlanDestination.
     * @param {object} models - The models to associate with.
     */
    Client.hasMany(models.CallPlanDestination, { foreignKey: "client_id", as: "callplandestinations" });

    /**
     * A Client has many CallPlanVisits.
     * Defines a one-to-many relationship between Client and CallPlanVisit.
     * @param {object} models - The models to associate with.
     */
    Client.hasMany(models.CallPlanVisit, { foreignKey: "client_id", as: "callplanvisits" });

    /**
     * A Client has many CallPlanRealizations.
     * Defines a one-to-many relationship between Client and CallPlanRealization.
     * @param {object} models - The models to associate with.
     */
    Client.hasMany(models.CallPlanRealization, { foreignKey: "client_id", as: "callplanrealizations" });

    Client.hasMany(models.MasterList, { foreignKey: "client_id", as: "masterlist_clients" });
    Client.hasMany(models.ClientCollabDetail, { foreignKey: "client_id", as: "clientcollabsdetails" });
    Client.hasMany(models.GimmickRequest, { foreignKey: "client_id", as: "gimmickrequests" });
    Client.hasMany(models.BankAccount, { foreignKey: "client_id", as: "bankaccounts" });
    Client.hasMany(models.UpCountryVisitList, { foreignKey: "client_id", as: "upcountryvisitlists" });
  };

  return Client;
};
