<?php

namespace FleekDash\Integrations\Core;

defined('ABSPATH') || exit;

/**
 * Base Service Provider for Integrations
 *
 * Provides a clean way to register integration services
 */
abstract class ServiceProvider
{
    protected Container $container;
    protected array $provides = [];
    protected bool $defer = false;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    /**
     * Register services in the container
     */
    abstract public function register(): void;

    /**
     * Boot services after all providers have been registered
     */
    public function boot(): void
    {
        // Override in child classes if needed
    }

    /**
     * Get the services provided by this provider
     */
    public function provides(): array
    {
        return $this->provides;
    }

    /**
     * Determine if the provider should be deferred
     */
    public function isDeferred(): bool
    {
        return $this->defer;
    }

    /**
     * Bind a service in the container
     */
    protected function bind(string $abstract, $concrete = null, bool $shared = false): void
    {
        $this->container->bind($abstract, $concrete, $shared);
    }

    /**
     * Bind a singleton in the container
     */
    protected function singleton(string $abstract, $concrete = null): void
    {
        $this->container->singleton($abstract, $concrete);
    }

    /**
     * Get an instance from the container
     */
    protected function make(string $abstract)
    {
        return $this->container->make($abstract);
    }

    /**
     * Register event listeners
     */
    protected function listen(string $event, callable $listener, int $priority = 0): void
    {
        $events = $this->container->make(EventDispatcher::class);
        $events->listen($event, $listener, $priority);
    }

    /**
     * Register WordPress hooks
     */
    protected function addAction(string $hook, callable $callback, int $priority = 10, int $args = 1): void
    {
        add_action($hook, $callback, $priority, $args);
    }

    /**
     * Register WordPress filters
     */
    protected function addFilter(string $hook, callable $callback, int $priority = 10, int $args = 1): void
    {
        add_filter($hook, $callback, $priority, $args);
    }

    /**
     * Register REST API routes
     */
    protected function registerRestRoute(string $namespace, string $route, array $args): void
    {
        add_action('rest_api_init', function () use ($namespace, $route, $args) {
            register_rest_route($namespace, $route, $args);
        });
    }

    /**
     * Enqueue scripts and styles efficiently
     */
    protected function enqueueAssets(string $handle, string $src, array $deps = [], string $version = null, bool $footer = true): void
    {
        $assetManager = $this->container->make(AssetManager::class);
        $assetManager->enqueue($handle, $src, $deps, $version, $footer);
    }

    /**
     * Load a view file
     */
    protected function view(string $view, array $data = []): string
    {
        return $this->container->make(ViewRenderer::class)->render($view, $data);
    }

    /**
     * Cache data efficiently
     */
    protected function cache(string $key, callable $callback, int $ttl = 3600)
    {
        return $this->container->make(Cache::class)->remember($key, $callback, $ttl);
    }
}
