<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /includes/class-google-channel.php
// Description: Google Business Profile social channel implementing ChannelInterface - NUCLEAR+++
// Last Edited: 2026-04-20
// ==================================================

namespace AutoMarketerAI;

// No direct access
defined('ABSPATH') || exit;

final class GoogleChannel implements ChannelInterface
{
    // --------------------------------------------------
    // ChannelInterface: unique channel slug
    // --------------------------------------------------
    public function get_slug(): string
    {
        return 'google';
    }

    // --------------------------------------------------
    // ChannelInterface: human-readable label for nav
    // --------------------------------------------------
    public function get_label(): string
    {
        return 'Google';
    }

    // --------------------------------------------------
    // ChannelInterface: brand hex color for nav indicator
    // --------------------------------------------------
    public function get_color(): string
    {
        return '#4285F4';
    }

    // --------------------------------------------------
    // ChannelInterface: availability -- Starter plan and above
    // --------------------------------------------------
    public function is_available(): bool
    {
        $plan = (string) get_option('automarketer_ai_plan_id', 'free');
        return in_array($plan, ['starter', 'starter_pro', 'unlimited', 'unlimited_ai', 'agency'], true);
    }

    // --------------------------------------------------
    // ChannelInterface: current connection state
    // --------------------------------------------------
    public function get_connection_status(): string
    {
        if (GoogleAuth::isConnected()) {
            return 'connected';
        }
        return 'disconnected';
    }

    // --------------------------------------------------
    // ChannelInterface: handle GET/POST actions (admin_init)
    // --------------------------------------------------
    public function handle_actions(): void
    {
        if (isset($_POST['amai_google_disconnect']) && check_admin_referer('amai_google_disconnect', '_wpnonce')) {
            GoogleAuth::disconnect();
            wp_safe_redirect(admin_url('admin.php?page=automarketer-ai-social&channel=google&amai_google_disconnected=1'));
            exit;
        }
    }

    // --------------------------------------------------
    // ChannelInterface: render the channel content area
    // --------------------------------------------------
    public function render(): void
    {
        if (GoogleAuth::isConnected()) {
            $this->renderDashboard();
        } else {
            $this->renderConnect();
        }
    }

    // --------------------------------------------------
    // Private: render the connection screen
    // --------------------------------------------------
    private function renderConnect(): void
    {
        $templatePath = AMAI_PLUGIN_DIR . 'templates/social/google-connect.php';
        if (is_file($templatePath)) {
            include $templatePath;
        }
    }

    // --------------------------------------------------
    // Private: render the dashboard
    // --------------------------------------------------
    private function renderDashboard(): void
    {
        $templatePath = AMAI_PLUGIN_DIR . 'templates/social/google-dashboard.php';
        if (is_file($templatePath)) {
            include $templatePath;
        }
    }
}