<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /includes/class-tiktok-channel.php
// Description: TikTok social channel - implements ChannelInterface, owns TikTok OAuth and render logic - NUCLEAR+++
// Last Edited: 2026-04-15
// ==================================================

namespace AutoMarketerAI;

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

final class TikTokChannel implements ChannelInterface
{
    public function get_slug(): string
    {
        return 'tiktok';
    }

    public function get_label(): string
    {
        return 'TikTok';
    }

    public function get_color(): string
    {
        return '#000000';
    }

    public function is_available(): bool
    {
        return false;
    }

    public function get_connection_status(): string
    {
        return TikTokAuth::getConnection() !== null ? 'connected' : 'disconnected';
    }

    public function handle_actions(): void
    {
        // --------------------------------------------------
        // Handle disconnect POST
        // --------------------------------------------------
        if (
            isset($_POST['amai_tiktok_disconnect'])
            && isset($_POST['_wpnonce'])
            && wp_verify_nonce(sanitize_text_field((string) $_POST['_wpnonce']), 'amai_tiktok_disconnect')
        ) {
            TikTokAuth::disconnect();
            wp_safe_redirect(
                admin_url('admin.php?page=automarketer-ai-social&channel=tiktok&disconnected=1')
            );
            exit;
        }
    }

    public function render(): void
    {
        if (TikTokAuth::getConnection() === null) {
            include AMAI_PLUGIN_DIR . 'templates/social/tiktok-connect.php';
            return;
        }

        include AMAI_PLUGIN_DIR . 'templates/social/tiktok-dashboard.php';
    }
}