import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
} from 'sequelize';
import type { Users } from './users';

export class Permissions extends Model<
  InferAttributes<Permissions>,
  InferCreationAttributes<Permissions>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  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>;


  declare getCreatedBy: BelongsToGetAssociationMixin<Users>;
  declare setCreatedBy: BelongsToSetAssociationMixin<Users, string>;
  declare getUpdatedBy: BelongsToGetAssociationMixin<Users>;
  declare setUpdatedBy: BelongsToSetAssociationMixin<Users, string>;

  static associate(db: Db): void {


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





























//end loop






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

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

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

name: {
        type: DataTypes.TEXT,
      
      

      },

      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: 'permissions',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Permissions;
}
