<?php

namespace FleekDash\Integrations\Core;

defined('ABSPATH') || exit;

/**
 * Modern Integration Interface
 */
interface IntegrationInterface
{
    public function getId(): string;
    public function getName(): string;
    public function getVersion(): string;
    public function isAvailable(): bool;
    public function activate(): void;
    public function deactivate(): void;
}

/**
 * Base Integration Class
 */
abstract class Integration implements IntegrationInterface
{
    protected Container $container;
    protected array $config;
    protected string $path;

    public function __construct(Container $container, array $config, string $path)
    {
        $this->container = $container;
        $this->config = $config;
        $this->path = $path;
    }

    public function getId(): string
    {
        return $this->config['id'];
    }

    public function getName(): string
    {
        return $this->config['name'] ?? $this->getId();
    }

    public function getVersion(): string
    {
        return $this->config['version'] ?? '1.0.0';
    }

    public function getConfig(): array
    {
        return $this->config;
    }

    public function getPath(): string
    {
        return $this->path;
    }

    /**
     * Get service provider class
     */
    abstract public function getServiceProvider(): string;

    /**
     * Check if integration is available
     */
    public function isAvailable(): bool
    {
        $requires = $this->config['requires'] ?? [];

        // Check requirements using discovery engine
        $discovery = $this->container->make(DiscoveryEngine::class);
        return $discovery->checkRequirements($requires);
    }

    /**
     * Activate integration
     */
    public function activate(): void
    {
        $active = get_option('fleekdash_active_integrations', []);
        $category = $this->config['category'] ?? 'plugin';

        if (!isset($active[$category])) {
            $active[$category] = [];
        }

        if (!in_array($this->getId(), $active[$category])) {
            $active[$category][] = $this->getId();
            update_option('fleekdash_active_integrations', $active);
        }

        // Fire event
        $events = $this->container->make(EventDispatcher::class);
        $events->dispatch('integration.activated', [
            'id' => $this->getId(),
            'category' => $category
        ]);
    }

    /**
     * Deactivate integration
     */
    public function deactivate(): void
    {
        $active = get_option('fleekdash_active_integrations', []);
        $category = $this->config['category'] ?? 'plugin';

        if (isset($active[$category])) {
            $active[$category] = array_diff($active[$category], [$this->getId()]);
            update_option('fleekdash_active_integrations', $active);
        }

        // Fire event
        $events = $this->container->make(EventDispatcher::class);
        $events->dispatch('integration.deactivated', [
            'id' => $this->getId(),
            'category' => $category
        ]);
    }

    /**
     * Check if integration is active
     */
    public function isActive(): bool
    {
        $active = get_option('fleekdash_active_integrations', []);
        $category = $this->config['category'] ?? 'plugin';

        return isset($active[$category]) && in_array($this->getId(), $active[$category]);
    }
}
