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

module.exports = (sequelize) => {
  const UpCountry = sequelize.define(
    "upcountrie",
    {
      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 },

      doc_no: { type: DataTypes.STRING, allowNull: false },
      from_date: { type: DataTypes.DATEONLY, allowNull: true },
      to_date: { type: DataTypes.DATEONLY, allowNull: true },
      place: { type: DataTypes.STRING, allowNull: true },
      scope: { type: DataTypes.STRING, allowNull: true },
      stay: { type: DataTypes.BOOLEAN, allowNull: true },
      activity: { type: DataTypes.TEXT, allowNull: true },
      visit_order: { type: DataTypes.INTEGER, allowNull: true },
      target: { type: DataTypes.STRING, allowNull: true },
      transport: { type: DataTypes.STRING, allowNull: true },
      transport_to_ga: { type: DataTypes.BOOLEAN, allowNull: true },
      accommodation: { type: DataTypes.STRING, allowNull: true },
      accommodation_to_ga: { type: DataTypes.BOOLEAN, allowNull: true },
      meal: { type: DataTypes.STRING, allowNull: true },
      total_distance: { type: DataTypes.DECIMAL, allowNull: true },
      is_event: { type: DataTypes.BOOLEAN, allowNull: true },

      // fk
      designation_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      territory_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      employee_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      callplan_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      department_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
    },
    {
      tableName: "upcountries",
      freezeTableName: true,
    }
  );

  UpCountry.associate = (models) => {
    UpCountry.belongsTo(models.Designation, { foreignKey: "designation_id", as: "designation" });
    UpCountry.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });
    UpCountry.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });
    UpCountry.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
    UpCountry.belongsTo(models.Department, { foreignKey: "department_id", as: "department" });
    UpCountry.belongsTo(models.CallPlan, { foreignKey: "callplan_id", as: "callplan" });
    UpCountry.hasMany(models.UpCountryDistance, { foreignKey: "upcountry_id", as: "upcountrydistances" });
    UpCountry.hasMany(models.UpCountryItinerary, { foreignKey: "upcountry_id", as: "upcountryitineraries" });
    UpCountry.hasMany(models.UpCountryVisitList, { foreignKey: "upcountry_id", as: "upcountryvisitlists" });
    UpCountry.hasMany(models.UpCountryAccommodation, { foreignKey: "upcountry_id", as: "upcountryaccommodations" });
    UpCountry.hasMany(models.UcCostRealization, { foreignKey: "upcountry_id", as: "uccostrealizations" });
  };

  return UpCountry;
};
