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

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

			name: { type: DataTypes.STRING(100), allowNull: false },
			display_name: { type: DataTypes.STRING(150), allowNull: true },
			slug_name: { type: DataTypes.STRING(100), allowNull: true },
			abbr: { type: DataTypes.STRING(100), allowNull: true },
			date_of_establishment: { type: DataTypes.DATEONLY, allowNull: true },
			date_of_incorporation: { type: DataTypes.DATEONLY, allowNull: true },
			date_of_commencement: { type: DataTypes.DATEONLY, allowNull: true },
			tax_id: { type: DataTypes.INTEGER, allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },
			type: { type: DataTypes.STRING(50), allowNull: true },
			default_holiday_list: { type: DataTypes.INTEGER, allowNull: true },

			fax: { type: DataTypes.STRING(100), allowNull: true },
			address: { type: DataTypes.TEXT, allowNull: true },
			website: { type: DataTypes.STRING(100), allowNull: true },
			email: { type: DataTypes.STRING(100), allowNull: true },
			phone: { type: DataTypes.STRING(100), allowNull: true },

			is_parent: { type: DataTypes.BOOLEAN, allowNull: true },
			parent_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "companies",
		},
		{
			freezeTableName: true,
		}
	);

	/**
	 * Define associations between Company and other models.
	 * @param {object} models - The models to associate with.
	 */
	Company.associate = (models) => {
		/**
		 * Associates the Company model with the User model.
		 * Defines a many-to-many relationship between Company and User.
		 * @param {object} models - The models to associate with.
		 */
		Company.belongsToMany(models.User, { through: models.UserCompany, foreignKey: "company_id", otherKey: "user_id", as: "users" });

		/**
		 * Associates the Company model with the Territory model.
		 * Defines a one-to-many relationship between Company and Territory.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Territory, { foreignKey: "company_id", as: "territories" });

		/**
		 * Associates the Company model with the TerritoryVersion model.
		 * Defines a one-to-many relationship between Company and TerritoryVersion.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.TerritoryVersion, { foreignKey: "company_id", as: "territoryversions" });

		/**
		 * Associates the Company model with the TerritoryVersionChild model.
		 * Defines a one-to-many relationship between Company and TerritoryVersionChild.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.TerritoryVersionChild, { foreignKey: "company_id", as: "territoryversionchilds" });

		/**
		 * Associates the Company model with the ClientType model.
		 * Defines a one-to-many relationship between Company and ClientType.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.ClientType, { foreignKey: "company_id", as: "clienttypes" });

		/**
		 * Associates the Company model with the Client model.
		 * Defines a one-to-many relationship between Company and Client.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Client, { foreignKey: "company_id", as: "clients" });

		/**
		 * Associates the Company model with the Department model.
		 * Defines a one-to-many relationship between Company and Department.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Department, { foreignKey: "company_id", as: "departments" });

		/**
		 * Associates the Company model with the Employee model.
		 * Defines a one-to-many relationship between Company and Employee.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Employee, { foreignKey: "company_id" });

		/**
		 * Associates the Company model with the Outlet model.
		 * Defines a one-to-many relationship between Company and Outlet.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Outlet, { foreignKey: "company_id", as: "outlets" });

		/**
		 * Associates the Company model with the Designation model.
		 * Defines a one-to-many relationship between Company and Designation.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Designation, { foreignKey: "company_id", as: "designations" });

		/**
		 * Associates the Company model with the HolidayGroup model.
		 * Defines a one-to-many relationship between Company and HolidayGroup.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.HolidayGroup, { foreignKey: "company_id", as: "holidaygroups" });

		/**
		 * Associates the Company model with the ItemCompetitor model.
		 * Defines a one-to-many relationship between Company and ItemCompetitor.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.ItemCompetitor, { foreignKey: "company_id", as: "itemcompetitors" });

		/**
		 * Associates the Company model with the ItemGroup model.
		 * Defines a one-to-many relationship between Company and ItemGroup.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.ItemGroup, { foreignKey: "company_id", as: "itemgroups" });

		/**
		 * Associates the Company model with the DeliveryTrip model.
		 * Defines a one-to-many relationship between Company and DeliveryTrip.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.DeliveryTrip, { foreignKey: "company_id", as: "deliverytrips" });

		/**
		 * Associates the Company model with the Vehicle model.
		 * Defines a one-to-many relationship between Company and Vehicle.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Vehicle, { foreignKey: "company_id", as: "vehicles" });

		/**  Associates the Company model with the CallPlan model.
		 *  Defines a one-to-many relationship between Company and CallPlan.
		 *   @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.CallPlan, { foreignKey: "company_id", as: "callplans" });

		/**
		 * Associates the Company model with the DesignationVersion model.
		 * Defines a one-to-many relationship between Company and DesignationVersion.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.DesignationVersion, { foreignKey: "company_id", as: "designationversions" });

		/**
		 * Associates the Company model with the DesignationVersionChild model.
		 * Defines a one-to-many relationship between Company and DesignationVersionChild.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.DesignationVersionChild, { foreignKey: "company_id", as: "designationversionchilds" });

		/**
		 * Associates the Company model with the EmployeeCheckin model.
		 * Defines a one-to-many relationship between Company and EmployeeCheckin.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.EmployeeCheckin, { foreignKey: "company_id", as: "employeecheckins" });

		/**
		 * Associates the Company model with the ShiftType model.
		 * Defines a one-to-many relationship between Company and ShiftType.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.ShiftType, { foreignKey: "company_id", as: "shifttypes" });

		/**
		 * Associates the Company model with the LeaveApplication model.
		 * Defines a one-to-many relationship between Company and LeaveApplication.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.LeaveApplication, { foreignKey: "company_id", as: "leaveapplications" });

		/**
		 * Associates the Company model with the AttendanceRequest model.
		 * Defines a one-to-many relationship between Company and AttendanceRequest.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.AttendanceRequest, { foreignKey: "company_id", as: "attendancerequests" });

		/**
		 * Associates the Company model with the Distributor model.
		 * Defines a one-to-many relationship between Company and Distributor.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Distributor, { foreignKey: "company_id", as: "distributors" });

		/**
		 * Associates the Company model with the LeaveType model.
		 * Defines a one-to-many relationship between Company and LeaveType.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.LeaveType, { foreignKey: "company_id", as: "leavetypes" });

		/**
		 * Associates the Company model with the EmployeeRequest model.
		 * Defines a one-to-many relationship between Company and EmployeeRequest.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.EmployeeRequest, { foreignKey: "company_id", as: "employeerequests" });

		/**
		 * Associates the Company model with the AbsenceClarification model.
		 * Defines a one-to-many relationship between Company and AbsenceClarification.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.AbsenceClarification, { foreignKey: "company_id", as: "absenceclarifications" });

		/**P
		 * Associates the Company model with the SalesDistributor model.
		 * Defines a one-to-many relationship between Company and SalesDistributor.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.SalesDistributor, { foreignKey: "company_id", as: "salesdistributors" });

		/**
		 * Associates the Company model with the ImportSalesDistributor model.
		 * Defines a one-to-many relationship between Company and ImportSalesDistributor.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.ImportSalesDistributor, { foreignKey: "company_id", as: "importsalesdistributors" });
		Company.hasMany(models.ClientCollab, { foreignKey: "company_id", as: "clientcollabs" });
		Company.hasMany(models.Survey, { foreignKey: "company_id", as: "surveys" });
		Company.hasMany(models.SurveySellingIn, { foreignKey: "company_id", as: "surveysellingins" });
		Company.hasMany(models.SalesTarget, { foreignKey: "company_id", as: "salestargets" });
		Company.hasMany(models.JobPosting, { foreignKey: "company_id", as: "jobpostings" });
		Company.hasMany(models.NcProduct, { foreignKey: "company_id", as: "ncproducts" });
		Company.hasMany(models.ProductReturn, { foreignKey: "company_id", as: "productreturns" });

		/**
		 * Associates the Company model with the CompanyAsset model.
		 * Defines a one-to-many relationship between Company and CompanyAsset.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.CompanyAsset, { foreignKey: "company_id", as: "companyassets" });

		/**
		 * Associates the Company model with the CompanySetting model.
		 * Defines a one-to-many relationship between Company and CompanySetting.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.CompanySetting, { foreignKey: "company_id", as: "companysettings" });

		/**
		 * Associates the Company model with the SalesPlan model.
		 * Defines a one-to-many relationship between Company and SalesPlan.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.SalesPlan, { foreignKey: "company_id", as: "salesplans" });

		/**
		 * Associates the Company model with the Role model.
		 * Defines a one-to-many relationship between Company and Role.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Role, { foreignKey: "company_id", as: "roles" });

		/**
		 * Associates the Company model with the InvoiceReceipt model.
		 * Defines a one-to-many relationship between Company and InvoiceReceipt.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.InvoiceReceipt, { foreignKey: "company_id", as: "invoicereceipts" });

		/**
		 * Associates the Company model with the Fse model.
		 * Defines a one-to-many relationship between Company and Fse.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Fse, { foreignKey: "company_id", as: "fses" });

		/**
		 * Associates the Company model with the Gender model.
		 * Defines a one-to-many relationship between Company and Gender.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.Gender, { foreignKey: "company_id", as: "genders" });

		/**
		 * Associates the Company model with the EmployeeStatus model.
		 * Defines a one-to-many relationship between Company and EmployeeStatus.
		 * @param {object} models - The models to associate with.
		 */
		Company.hasMany(models.EmployeeStatus, { foreignKey: "company_id", as: "employeestatuses" });

		Company.hasMany(models.BankAccount, { foreignKey: "company_id", as: "bankaccounts" });

		Company.hasMany(models.Age, { foreignKey: "company_id", as: "ages" });

		Company.hasMany(models.BodyTemperature, { foreignKey: "company_id", as: "bodytemperatures" });

		Company.hasMany(models.Pulse, { foreignKey: "company_id", as: "pulses" });

		Company.hasMany(models.RespiratoryRate, { foreignKey: "company_id", as: "respiratoryrates" });

		Company.hasMany(models.GimmickRequest, { foreignKey: "company_id", as: "gimmickrequests" });

		Company.hasMany(models.MaritalStatus, { foreignKey: "company_id", as: "maritalstatuses" });

		Company.hasMany(models.BloodPressure, { foreignKey: "company_id", as: "bloodpressures" });

		Company.hasMany(models.PulseAgeRate, { foreignKey: "company_id", as: "pulseagerates" });

		Company.hasMany(models.Branch, { foreignKey: "company_id", as: "branhces" });

		Company.hasMany(models.EmployeeGrade, { foreignKey: "company_id", as: "employeegrades" });

		Company.hasMany(models.EmploymentType, { foreignKey: "company_id", as: "employmenttypes" });

		Company.hasMany(models.EmployeeGroupTable, { foreignKey: "company_id", as: "employeegrouptables" });

		Company.hasMany(models.LeavePeriod, { foreignKey: "company_id", as: "leaveperiods" });

		Company.hasMany(models.RegCategories, { foreignKey: "company_id", as: "regcategories" });

		Company.hasMany(models.DocumentGroup, { foreignKey: "company_id", as: "documentgroups" });

		Company.hasMany(models.DMSRegulatory, { foreignKey: "company_id", as: "dmsregulatories" });

		Company.hasMany(models.TrackingLocation, { foreignKey: "company_id", as: "trackinglocations" });

		Company.hasMany(models.Patient, { foreignKey: "company_id", as: "patients" });

		Company.hasMany(models.UpCountry, { foreignKey: "company_id", as: "upcountries" });
	};

	return Company;
};
