import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type NonAttribute,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import { ROLE_SCOPE_VALUES, type RoleScope } from '@/shared/constants/roles';
import type {
  BelongsToGetAssociationMixin,
  BelongsToManyGetAssociationsMixin,
  BelongsToManySetAssociationsMixin,
  BelongsToSetAssociationMixin,
  HasManyGetAssociationsMixin,
  HasManySetAssociationsMixin,
} from 'sequelize';
import type { Permissions } from './permissions';
import type { Users } from './users';

export class Roles extends Model<
  InferAttributes<Roles>,
  InferCreationAttributes<Roles>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  /** Authorization scope (Workstream 3 §3.1). Required on every role. */
  declare scope: CreationOptional<RoleScope>;
  declare globalAccess: CreationOptional<boolean>;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;

  // Eager-loaded association (populated by `include`).
  declare permissions?: NonAttribute<Permissions[]>;

  declare getPermissions: BelongsToManyGetAssociationsMixin<Permissions>;
  declare setPermissions: BelongsToManySetAssociationsMixin<Permissions, string>;
  declare getPermissions_filter: BelongsToManyGetAssociationsMixin<Permissions>;
  declare setPermissions_filter: BelongsToManySetAssociationsMixin<Permissions, string>;
  declare getUsers_app_role: HasManyGetAssociationsMixin<Users>;
  declare setUsers_app_role: HasManySetAssociationsMixin<Users, string>;
  declare getCreatedBy: BelongsToGetAssociationMixin<Users>;
  declare setCreatedBy: BelongsToSetAssociationMixin<Users, string>;
  declare getUpdatedBy: BelongsToGetAssociationMixin<Users>;
  declare setUpdatedBy: BelongsToSetAssociationMixin<Users, string>;

  static associate(db: Db): void {

    db.roles.belongsToMany(db.permissions, {
      as: 'permissions',
      foreignKey: {
        name: 'roles_permissionsId',
      },
      constraints: false,
      through: 'rolesPermissionsPermissions',
    });

    db.roles.belongsToMany(db.permissions, {
      as: 'permissions_filter',
      foreignKey: {
        name: 'roles_permissionsId',
      },
      constraints: false,
      through: 'rolesPermissionsPermissions',
    });


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity


    db.roles.hasMany(db.users, {
      as: 'users_app_role',
      foreignKey: {
          name: 'app_roleId',
      },
      constraints: false,
    });




























//end loop






    db.roles.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.roles.belongsTo(db.users, {
      as: 'updatedBy',
    });
  }
}

export default function (sequelize: Sequelize): typeof Roles {
  Roles.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,



      },

      scope: {
        type: DataTypes.ENUM(...ROLE_SCOPE_VALUES),
        allowNull: false,
      },

globalAccess: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'roles',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Roles;
}
