// 导入数据库配置和所有模型
const { sequelize } = require('../config/database');
const User = require('./User');
const Role = require('./Role');
const Permission = require('./Permission');
const RolePermission = require('./RolePermission');
const MenuPermission = require('./MenuPermission');
const InsuranceApplication = require('./InsuranceApplication');
const InsuranceType = require('./InsuranceType');
const Policy = require('./Policy');
const Claim = require('./Claim');
const Menu = require('./Menu');
const SupervisoryTask = require('./SupervisoryTask');
const InstallationTask = require('./InstallationTask');
const LivestockType = require('./LivestockType');
const LivestockPolicy = require('./LivestockPolicy');
const LivestockClaim = require('./LivestockClaim');
const Device = require('./Device');
const DeviceAlert = require('./DeviceAlert');
const OperationLog = require('./OperationLog');

// 定义模型关联关系

// 用户和角色关联
User.belongsTo(Role, { foreignKey: 'role_id', as: 'role' });
Role.hasMany(User, { foreignKey: 'role_id', as: 'users' });

// 角色和权限多对多关联（通过中间表）
Role.belongsToMany(Permission, { 
  through: RolePermission, 
  foreignKey: 'role_id',
  otherKey: 'permission_id',
  as: 'rolePermissions' 
});
Permission.belongsToMany(Role, { 
  through: RolePermission, 
  foreignKey: 'permission_id',
  otherKey: 'role_id',
  as: 'permissionRoles' 
});

// 角色权限关联表的直接关联
RolePermission.belongsTo(Role, { foreignKey: 'role_id', as: 'role' });
RolePermission.belongsTo(Permission, { foreignKey: 'permission_id', as: 'permission' });
Role.hasMany(RolePermission, { foreignKey: 'role_id', as: 'rolePermissionRecords' });
Permission.hasMany(RolePermission, { foreignKey: 'permission_id', as: 'permissionRoleRecords' });

// 菜单和权限多对多关联（通过中间表）
Menu.belongsToMany(Permission, { 
  through: MenuPermission, 
  foreignKey: 'menu_id',
  otherKey: 'permission_id',
  as: 'permissions' 
});
Permission.belongsToMany(Menu, { 
  through: MenuPermission, 
  foreignKey: 'permission_id',
  otherKey: 'menu_id',
  as: 'menus' 
});

// 菜单权限关联表的直接关联
MenuPermission.belongsTo(Menu, { foreignKey: 'menu_id', as: 'menu' });
MenuPermission.belongsTo(Permission, { foreignKey: 'permission_id', as: 'permission' });
Menu.hasMany(MenuPermission, { foreignKey: 'menu_id', as: 'menuPermissions' });
Permission.hasMany(MenuPermission, { foreignKey: 'permission_id', as: 'menuPermissions' });

// 保险申请和保险类型关联
InsuranceApplication.belongsTo(InsuranceType, { 
  foreignKey: 'insurance_type_id', 
  as: 'insurance_type' 
});
InsuranceType.hasMany(InsuranceApplication, { 
  foreignKey: 'insurance_type_id', 
  as: 'applications' 
});

// 保险申请和审核人关联
InsuranceApplication.belongsTo(User, { 
  foreignKey: 'reviewer_id', 
  as: 'reviewer' 
});
User.hasMany(InsuranceApplication, { 
  foreignKey: 'reviewer_id', 
  as: 'reviewed_applications' 
});

// 保单和保险申请关联
Policy.belongsTo(InsuranceApplication, { 
  foreignKey: 'application_id', 
  as: 'application' 
});
InsuranceApplication.hasOne(Policy, { 
  foreignKey: 'application_id', 
  as: 'policy' 
});

// 保单和客户关联
Policy.belongsTo(User, { 
  foreignKey: 'customer_id', 
  as: 'customer' 
});
User.hasMany(Policy, { 
  foreignKey: 'customer_id', 
  as: 'policies' 
});

// 保单和保险类型关联
Policy.belongsTo(InsuranceType, { 
  foreignKey: 'insurance_type_id', 
  as: 'insurance_type' 
});
InsuranceType.hasMany(Policy, { 
  foreignKey: 'insurance_type_id', 
  as: 'policies' 
});

// 理赔和保单关联
Claim.belongsTo(Policy, { 
  foreignKey: 'policy_id', 
  as: 'policy' 
});
Policy.hasMany(Claim, { 
  foreignKey: 'policy_id', 
  as: 'claims' 
});

// 理赔和客户关联
Claim.belongsTo(User, { 
  foreignKey: 'customer_id', 
  as: 'customer' 
});
User.hasMany(Claim, { 
  foreignKey: 'customer_id', 
  as: 'customer_claims' 
});

// 理赔和审核人关联
Claim.belongsTo(User, { 
  foreignKey: 'reviewer_id', 
  as: 'reviewer' 
});
User.hasMany(Claim, { 
  foreignKey: 'reviewer_id', 
  as: 'reviewed_claims' 
});

// 监管任务关联
SupervisoryTask.belongsTo(User, {
  foreignKey: 'assignedTo',
  as: 'assignedUser'
});
SupervisoryTask.belongsTo(User, {
  foreignKey: 'createdBy',
  as: 'creator'
});
SupervisoryTask.belongsTo(User, {
  foreignKey: 'updatedBy',
  as: 'updater'
});

// 待安装任务关联
InstallationTask.belongsTo(User, {
  foreignKey: 'assignedTo',
  as: 'technician'
});
InstallationTask.belongsTo(User, {
  foreignKey: 'createdBy',
  as: 'creator'
});
InstallationTask.belongsTo(User, {
  foreignKey: 'updatedBy',
  as: 'updater'
});

// 生资保险相关关联
// 注意：牲畜类型表中没有created_by和updated_by字段，所以不定义这些关联

// 生资保单和牲畜类型关联
LivestockPolicy.belongsTo(LivestockType, {
  foreignKey: 'livestock_type_id',
  as: 'livestock_type'
});
LivestockType.hasMany(LivestockPolicy, {
  foreignKey: 'livestock_type_id',
  as: 'policies'
});

// 生资保单和用户关联
LivestockPolicy.belongsTo(User, {
  foreignKey: 'created_by',
  as: 'creator'
});
LivestockPolicy.belongsTo(User, {
  foreignKey: 'updated_by',
  as: 'updater'
});

// 生资理赔和生资保单关联
LivestockClaim.belongsTo(LivestockPolicy, {
  foreignKey: 'policy_id',
  as: 'policy'
});
LivestockPolicy.hasMany(LivestockClaim, {
  foreignKey: 'policy_id',
  as: 'claims'
});

// 生资理赔和用户关联
LivestockClaim.belongsTo(User, {
  foreignKey: 'created_by',
  as: 'creator'
});
LivestockClaim.belongsTo(User, {
  foreignKey: 'reviewer_id',
  as: 'reviewer'
});

// 设备和用户关联
Device.belongsTo(User, {
  foreignKey: 'created_by',
  as: 'creator'
});
Device.belongsTo(User, {
  foreignKey: 'updated_by',
  as: 'updater'
});

// 设备预警和设备关联
DeviceAlert.belongsTo(Device, {
  foreignKey: 'device_id',
  as: 'device'
});
Device.hasMany(DeviceAlert, {
  foreignKey: 'device_id',
  as: 'alerts'
});

// 设备预警和用户关联
DeviceAlert.belongsTo(User, {
  foreignKey: 'handler_id',
  as: 'handler'
});
User.hasMany(DeviceAlert, {
  foreignKey: 'handler_id',
  as: 'handled_alerts'
});

// 操作日志和用户关联
OperationLog.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});
User.hasMany(OperationLog, {
  foreignKey: 'user_id',
  as: 'operation_logs'
});

// 导出所有模型
module.exports = {
  sequelize,
  User,
  Role,
  Permission,
  RolePermission,
  MenuPermission,
  InsuranceApplication,
  InsuranceType,
  Policy,
  Claim,
  Menu,
  SupervisoryTask,
  InstallationTask,
  LivestockType,
  LivestockPolicy,
  LivestockClaim,
  Device,
  DeviceAlert,
  OperationLog
};