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

module.exports = (sequelize) => {
	const Asset = sequelize.define(
		"asset",
		{
			id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
			creation: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
			modified: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
			modified_by: { type: DataTypes.INTEGER, allowNull: false },
			owner: { type: DataTypes.INTEGER, allowNull: false },
			createdAt: { type: DataTypes.DATE, allowNull: true, field: "createdAt" },
			updatedAt: { type: DataTypes.DATE, allowNull: true, field: "updatedAt" },
			enabled: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },

			asset_tag: { type: DataTypes.STRING(100), allowNull: false, unique: true },
			serial: { type: DataTypes.STRING(150), allowNull: true },
			name: { type: DataTypes.STRING(150), allowNull: true },
			model_id: { type: DataTypes.INTEGER, allowNull: false },
			status_id: { type: DataTypes.INTEGER, allowNull: false },
			location_id: { type: DataTypes.INTEGER, allowNull: true },
			rtd_location_id: { type: DataTypes.INTEGER, allowNull: true },

			// Polymorphic Assignment
			assigned_to: { type: DataTypes.INTEGER, allowNull: true },
			assigned_type: { type: DataTypes.STRING(50), allowNull: true }, // Employee, Department, Location
			assigned_at: { type: DataTypes.DATE, allowNull: true },

			purchase_date: { type: DataTypes.DATEONLY, allowNull: true },
			purchase_cost: { type: DataTypes.DECIMAL(10, 2), allowNull: true },
			order_number: { type: DataTypes.STRING(100), allowNull: true },
			notes: { type: DataTypes.TEXT, allowNull: true },
			image: { type: DataTypes.STRING(255), allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			erp_id: { type: DataTypes.STRING(150), allowNull: true },
		},
		{
			tableName: "assets",
			freezeTableName: true,
			indexes: [
				{ name: "idx_assets_tag", unique: true, fields: ["asset_tag"] },
				{ name: "idx_assets_serial", fields: ["serial"] },
				{ name: "idx_assets_name", fields: ["name"] },
				{ name: "idx_assets_company_id", fields: ["company_id"] },
				{ name: "idx_assets_status_id", fields: ["status_id"] },
				{ name: "idx_assets_model_id", fields: ["model_id"] },
				{ name: "idx_assets_location_id", fields: ["location_id"] },
				{ name: "idx_assets_assigned", fields: ["assigned_to", "assigned_type"] },
			],
		}
	);

	Asset.associate = (models) => {
		Asset.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		Asset.belongsTo(models.AssetModel, { foreignKey: "model_id", as: "model" });
		Asset.belongsTo(models.AssetStatusLabel, { foreignKey: "status_id", as: "status" });
		Asset.belongsTo(models.AssetLocation, { foreignKey: "location_id", as: "location" });
		Asset.belongsTo(models.AssetLocation, { foreignKey: "rtd_location_id", as: "defaultLocation" });

		// Dynamic associations for checkout targets
		Asset.belongsTo(models.Employee, { foreignKey: "assigned_to", constraints: false, as: "assignedEmployee" });
		Asset.belongsTo(models.Department, { foreignKey: "assigned_to", constraints: false, as: "assignedDepartment" });
		Asset.belongsTo(models.AssetLocation, { foreignKey: "assigned_to", constraints: false, as: "assignedLocation" });
	};

	return Asset;
};
