import { getGlobalHooks } from "../plugins/hook-system";
class EntityHookManager {
  hooks = getGlobalHooks();
  /**
   * Hook: Before entity creation
   * Allows plugins to validate, modify, or prevent entity creation
   */
  async beforeEntityCreate(entityName, data, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      data,
      userId,
      operation: "create"
    };
    const filteredData = await this.hooks.applyFilters(
      `entity.${entityName}.before_create`,
      hookData
    );
    await this.hooks.doAction(
      `entity.${entityName}.creating`,
      filteredData
    );
    return filteredData.data;
  }
  /**
   * Hook: After entity creation
   * Allows plugins to react to entity creation events
   */
  async afterEntityCreate(entityName, data, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      data,
      userId,
      operation: "create"
    };
    await this.hooks.doAction(
      `entity.${entityName}.created`,
      hookData
    );
    await this.hooks.doAction("entity.created", hookData);
  }
  /**
   * Hook: Before entity update
   */
  async beforeEntityUpdate(entityName, id, changes, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      id,
      changes,
      userId,
      operation: "update"
    };
    const filteredData = await this.hooks.applyFilters(
      `entity.${entityName}.before_update`,
      hookData
    );
    await this.hooks.doAction(
      `entity.${entityName}.updating`,
      filteredData
    );
    return filteredData.changes;
  }
  /**
   * Hook: After entity update
   */
  async afterEntityUpdate(entityName, id, data, changes, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      id,
      data,
      changes,
      userId,
      operation: "update"
    };
    await this.hooks.doAction(
      `entity.${entityName}.updated`,
      hookData
    );
    await this.hooks.doAction("entity.updated", hookData);
  }
  /**
   * Hook: Before entity deletion
   */
  async beforeEntityDelete(entityName, id, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      id,
      userId,
      operation: "delete"
    };
    const filteredData = await this.hooks.applyFilters(
      `entity.${entityName}.before_delete`,
      { ...hookData, allowDelete: true }
    );
    await this.hooks.doAction(
      `entity.${entityName}.deleting`,
      filteredData
    );
    return filteredData.allowDelete !== false;
  }
  /**
   * Hook: After entity deletion
   */
  async afterEntityDelete(entityName, id, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      id,
      userId,
      operation: "delete"
    };
    await this.hooks.doAction(
      `entity.${entityName}.deleted`,
      hookData
    );
    await this.hooks.doAction("entity.deleted", hookData);
  }
  /**
   * Hook: Entity validation
   * Allows plugins to add custom validation rules
   */
  async validateEntity(entityName, data, operation) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      data,
      operation,
      validation: { valid: true, errors: [] }
    };
    const result = await this.hooks.applyFilters(
      `entity.${entityName}.validate`,
      hookData
    );
    const genericResult = await this.hooks.applyFilters(
      "entity.validate",
      result
    );
    return {
      valid: genericResult.validation.valid,
      errors: genericResult.validation.errors,
      data: genericResult.data
    };
  }
  /**
   * Hook: Before entity query/read
   */
  async beforeEntityRead(entityName, query, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      query,
      userId,
      operation: "read"
    };
    const filteredData = await this.hooks.applyFilters(
      `entity.${entityName}.before_read`,
      hookData
    );
    return filteredData.query;
  }
  /**
   * Hook: After entity query/read
   */
  async afterEntityRead(entityName, results, query, userId) {
    const hookData = {
      entityName,
      entityConfig: this.getEntityConfig(entityName),
      results,
      query,
      userId,
      operation: "read"
    };
    const filteredData = await this.hooks.applyFilters(
      `entity.${entityName}.after_read`,
      hookData
    );
    return filteredData.results;
  }
  /**
   * Authentication hooks
   */
  async onUserLogin(user) {
    await this.hooks.doAction("auth.user.login", { user });
  }
  async onUserLogout(user) {
    await this.hooks.doAction("auth.user.logout", { user });
  }
  async onUserRegister(user) {
    await this.hooks.doAction("auth.user.register", { user });
  }
  /**
   * Get entity configuration (placeholder)
   */
  getEntityConfig(entityName) {
    return {
      name: entityName,
      displayName: entityName,
      pluralName: `${entityName}s`,
      // Placeholder values to satisfy the interface
      icon: null,
      fields: [],
      features: { enabled: true },
      permissions: {
        actions: [
          { action: "read", label: "Read", roles: [] },
          { action: "create", label: "Create", roles: [] },
          { action: "update", label: "Update", roles: [] },
          { action: "delete", label: "Delete", roles: [], dangerous: true }
        ]
      },
      planLimits: { free: {}, starter: {}, premium: {} },
      routes: { list: "", detail: "" },
      hooks: {},
      database: { table: entityName },
      api: { enabled: true }
    };
  }
}
const entityHookManager = new EntityHookManager();
const beforeEntityCreate = (entityName, data, userId) => entityHookManager.beforeEntityCreate(entityName, data, userId);
const afterEntityCreate = (entityName, data, userId) => entityHookManager.afterEntityCreate(entityName, data, userId);
const beforeEntityUpdate = (entityName, id, changes, userId) => entityHookManager.beforeEntityUpdate(entityName, id, changes, userId);
const afterEntityUpdate = (entityName, id, data, changes, userId) => entityHookManager.afterEntityUpdate(entityName, id, data, changes, userId);
const beforeEntityDelete = (entityName, id, userId) => entityHookManager.beforeEntityDelete(entityName, id, userId);
const afterEntityDelete = (entityName, id, userId) => entityHookManager.afterEntityDelete(entityName, id, userId);
const validateEntity = (entityName, data, operation) => entityHookManager.validateEntity(entityName, data, operation);
const beforeEntityRead = (entityName, query, userId) => entityHookManager.beforeEntityRead(entityName, query, userId);
const afterEntityRead = (entityName, results, query, userId) => entityHookManager.afterEntityRead(entityName, results, query, userId);
export {
  EntityHookManager,
  afterEntityCreate,
  afterEntityDelete,
  afterEntityRead,
  afterEntityUpdate,
  beforeEntityCreate,
  beforeEntityDelete,
  beforeEntityRead,
  beforeEntityUpdate,
  entityHookManager,
  validateEntity
};
