<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /includes/class-x-channel.php
// Description: X (Twitter) social channel module implementing ChannelInterface - NUCLEAR+++
// Last Edited: 2026-04-16
// ==================================================

namespace AutoMarketerAI;

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

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

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

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

    // --------------------------------------------------
    // ChannelInterface: whether this channel is built and available
    // --------------------------------------------------
    public function is_available(): bool
    {
        return true;
    }

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

        return 'disconnected';
    }

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

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

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

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