<?php

use Core\System\Router;

$_FB = ROOT_PATH . '/plugins/FacebookAds';
if (!class_exists('PluginLicenseGuard')) {
    require_once __DIR__ . '/PluginLicenseGuard.php';
}
require_once $_FB . '/LicenseGuard.php';

// ── License Check ──────────────────────────────────────────────────
try {
    $_fbKey = db()->query("SELECT license_key FROM fb_settings LIMIT 1")->fetchColumn();
    PluginLicenseGuard::check('facebook-ads', (string)$_fbKey);
} catch (\Throwable $_fbEx) { /* table not yet created during install */ }
require_once $_FB . '/FbLang.php';
require_once $_FB . '/Controllers/FbAdsAdminController.php';
require_once $_FB . '/Controllers/FbAdsApiController.php';
require_once $_FB . '/Controllers/FbAdsAnalyticsController.php';
require_once $_FB . '/Controllers/FbAdsAgentController.php';

Router::group(['prefix' => 'admin', 'namespace' => ''], function () {

    // ── Dashboard ─────────────────────────────────────────
    Router::get('/facebook-ads',                        'FbAdsAdminController@index');

    // ── Create Ad Wizard ──────────────────────────────────
    Router::get('/facebook-ads/create/{id:\d+}',        'FbAdsAdminController@create');
    Router::post('/facebook-ads/launch',                'FbAdsAdminController@launch');

    // ── Campaign management ───────────────────────────────
    Router::get('/facebook-ads/campaign/{id:\d+}',      'FbAdsAdminController@viewCampaign');
    Router::post('/facebook-ads/campaign/{id:\d+}/pause',  'FbAdsAdminController@pauseCampaign');
    Router::post('/facebook-ads/campaign/{id:\d+}/resume', 'FbAdsAdminController@resumeCampaign');
    Router::post('/facebook-ads/campaign/{id:\d+}/delete', 'FbAdsAdminController@deleteCampaign');
    Router::post('/facebook-ads/sync-stats',            'FbAdsAdminController@syncStats');

    // ── Analytics & AI Advisor ────────────────────────────
    Router::get('/facebook-ads/analytics/{id:\d+}',        'FbAdsAnalyticsController@index');
    Router::post('/facebook-ads/fetch-insights/{id:\d+}',  'FbAdsAnalyticsController@fetchInsights');
    Router::post('/facebook-ads/ai-advisor/{id:\d+}',      'FbAdsAnalyticsController@runAiAnalysis');
    Router::get('/facebook-ads/ai-advisor/{id:\d+}',       'FbAdsAnalyticsController@aiAdvisorPage');

    // ── AI helpers ────────────────────────────────────────
    Router::post('/facebook-ads/generate-copy',         'FbAdsApiController@generateCopy');
    Router::post('/facebook-ads/suggest-audience',      'FbAdsApiController@suggestAudience');

    // ── Settings ──────────────────────────────────────────
    Router::get('/facebook-ads/settings',               'FbAdsAdminController@settings');
    Router::post('/facebook-ads/settings',              'FbAdsAdminController@saveSettings');
    Router::post('/facebook-ads/verify-token',          'FbAdsAdminController@verifyToken');
    Router::post('/facebook-ads/rebind-license',        'FbAdsAdminController@rebindLicense');
    Router::post('/facebook-ads/verify-license',        'FbAdsAdminController@verifyLicense');

    // ── AI Agent (Autonomous) ─────────────────────────────
    Router::get('/facebook-ads/agent',                      'FbAdsAgentController@index');
    Router::post('/facebook-ads/agent/analyze',             'FbAdsAgentController@analyze');
    Router::post('/facebook-ads/agent/launch',              'FbAdsAgentController@launch');
    Router::post('/facebook-ads/agent/optimize/{id:\d+}',  'FbAdsAgentController@optimize');
    Router::post('/facebook-ads/uninstall',             'FbAdsAdminController@uninstall');
});

// ── Reflection: push before /{slug} catch-all ─────────────
try {
    $ref = new ReflectionProperty(\Core\System\Router::class, 'routes');
    $ref->setAccessible(true);
    $routes = $ref->getValue(null);
    $mine   = array_splice($routes, -22);
    $routes = array_merge($mine, $routes);
    $ref->setValue(null, $routes);
} catch (\Throwable $e) {}
