<?php

namespace Modules\SaasSwitcher\Http\Controllers;

use Illuminate\Routing\Controller;
use Modules\SaasSwitcher\Support\ProjectRegistry;

/**
 * Core-ERP integration hooks for the SaaS switcher.
 *
 * get_additional_script() is discovered by the ERP (ModuleUtil::getModuleData)
 * and its returned `additional_html` is injected into every admin layout. We
 * use it to show a small "you are inside project X — switch" banner while a
 * super-admin is impersonating a project, with no edit to any core view.
 */
class DataController extends Controller
{
    public function get_additional_script()
    {
        if (! session(config('saasswitcher.impersonating_key', 'saas_impersonating'))) {
            return [];
        }

        $key = session(config('saasswitcher.session_key', 'saas_project'));
        $project = ProjectRegistry::get((string) $key);
        if (! $project) {
            return [];
        }

        $label = e($project['label']);
        $icon  = e($project['icon'] ?? 'fas fa-layer-group');
        $color = e($project['color'] ?? '#6366f1');
        $switch = route('saas.exit');

        $html = <<<HTML
<div id="saas-project-bar" style="position:fixed;left:16px;bottom:16px;z-index:2147483000;
     display:flex;align-items:center;gap:10px;background:#0b1220;color:#e2e8f0;
     border:1px solid #1e293b;border-left:4px solid {$color};border-radius:10px;
     padding:8px 12px;font:600 12px/1.2 'Segoe UI',system-ui,sans-serif;
     box-shadow:0 8px 24px rgba(0,0,0,.35)">
  <span style="width:22px;height:22px;border-radius:6px;display:inline-flex;align-items:center;
        justify-content:center;background:{$color};color:#fff"><i class="{$icon}"></i></span>
  <span style="opacity:.7;font-weight:500">SaaS project:</span>
  <span>{$label}</span>
  <a href="{$switch}" style="margin-left:6px;background:{$color};color:#fff;text-decoration:none;
     padding:5px 10px;border-radius:6px"><i class="fas fa-right-left"></i> Switch project</a>
</div>
HTML;

        return ['additional_html' => $html];
    }
}
