const Admins = require("./Admins");
const RefreshToken = require("./RefreshTokens");
const Devices = require("./Devices");
const Employees = require("./Employees");
const EmployeeDevice = require("./EmployeeDevice");
const RcAccess = require("./RcAccess");

// ── Associations ──────────────────────────────────────────────────────────────

// Admin → RefreshTokens
Admins.hasMany(RefreshToken, { foreignKey: "adminId", onDelete: "CASCADE" });
RefreshToken.belongsTo(Admins, { foreignKey: "adminId" });

// Admin → Devices (who created it)
Admins.hasMany(Devices, { foreignKey: "createdBy", onDelete: "SET NULL" });
Devices.belongsTo(Admins, { foreignKey: "createdBy", as: "creator" });

// Admin → Employees (who created it)
Admins.hasMany(Employees, { foreignKey: "createdBy", onDelete: "SET NULL" });
Employees.belongsTo(Admins, { foreignKey: "createdBy", as: "creator" });

// Employee ↔ Device (many-to-many through EmployeeDevice)
Employees.belongsToMany(Devices, { through: EmployeeDevice, foreignKey: "employeeId" });
Devices.belongsToMany(Employees, { through: EmployeeDevice, foreignKey: "deviceId" });
Employees.hasMany(EmployeeDevice, { foreignKey: "employeeId" });
EmployeeDevice.belongsTo(Employees, { foreignKey: "employeeId" });
Devices.hasMany(EmployeeDevice, { foreignKey: "deviceId" });
EmployeeDevice.belongsTo(Devices, { foreignKey: "deviceId" });

module.exports = { Admins, RefreshToken, Devices, Employees, EmployeeDevice, RcAccess };