<?php

declare(strict_types=1);

namespace App\Http\Controllers\Account\Dedicated;

use App\Http\Controllers\Controller;
use App\Models\Service;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
use Inertia\Response;

class DedicatedPanelController extends Controller
{
    public function dashboard(Request $request, Service $service): Response
    {
        $this->authorize($request, $service);

        $hardwareInfo = $this->safeApiCall(function () use ($service): array {
            return $this->fetchHardwareInfo($service);
        });

        return Inertia::render('Services/Dedicated/Dashboard', [
            'service' => $service->load('plan', 'subscription'),
            'hardwareInfo' => $hardwareInfo ?? [
                'cpu' => null,
                'memory' => null,
                'disks' => [],
                'network_ports' => [],
            ],
        ]);
    }

    public function console(Request $request, Service $service): Response
    {
        $this->authorize($request, $service);

        $consoleUrl = $this->safeApiCall(function () use ($service): ?string {
            return $this->fetchConsoleUrl($service);
        });

        return Inertia::render('Services/Dedicated/Console', [
            'service' => $service->load('plan'),
            'consoleUrl' => $consoleUrl,
        ]);
    }

    public function bandwidth(Request $request, Service $service): Response
    {
        $this->authorize($request, $service);

        $bandwidthData = $this->safeApiCall(function () use ($service): array {
            return $this->fetchBandwidthData($service);
        });

        return Inertia::render('Services/Dedicated/Bandwidth', [
            'service' => $service->load('plan'),
            'bandwidthData' => $bandwidthData ?? [],
        ]);
    }

    public function network(Request $request, Service $service): Response
    {
        $this->authorize($request, $service);

        $networkData = $this->safeApiCall(function () use ($service): array {
            return $this->fetchNetworkData($service);
        });

        return Inertia::render('Services/Dedicated/Network', [
            'service' => $service->load('plan'),
            'networkData' => $networkData ?? [
                'ips' => [],
                'vlans' => [],
            ],
        ]);
    }

    public function reinstall(Request $request, Service $service): Response
    {
        $this->authorize($request, $service);

        $templates = $this->safeApiCall(function () use ($service): array {
            return $this->fetchTemplates($service);
        });

        return Inertia::render('Services/Dedicated/Reinstall', [
            'service' => $service->load('plan'),
            'templates' => $templates ?? [],
        ]);
    }

    public function settings(Request $request, Service $service): Response
    {
        $this->authorize($request, $service);

        return Inertia::render('Services/Dedicated/Settings', [
            'service' => $service->load('plan'),
        ]);
    }

    private function authorize(Request $request, Service $service): void
    {
        abort_unless($service->user_id === $request->user()->id, 403);
        abort_unless($service->service_type === 'dedicated', 404);
    }

    /**
     * @template T
     *
     * @param  callable(): T  $callback
     * @return T|null
     */
    private function safeApiCall(callable $callback): mixed
    {
        try {
            return $callback();
        } catch (\Exception $e) {
            Log::warning('Dedicated Panel API call failed', [
                'error' => $e->getMessage(),
            ]);

            return null;
        }
    }

    /**
     * @return array<string, mixed>
     */
    private function fetchHardwareInfo(Service $service): array
    {
        // SynergyCP API: GET /api/v1/servers/{id}/hardware
        return [
            'cpu' => null,
            'memory' => null,
            'disks' => [],
            'network_ports' => [],
        ];
    }

    private function fetchConsoleUrl(Service $service): ?string
    {
        // SynergyCP API: GET /api/v1/servers/{id}/ipmi/console
        return null;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    private function fetchBandwidthData(Service $service): array
    {
        // SynergyCP API: GET /api/v1/servers/{id}/bandwidth
        return [];
    }

    /**
     * @return array<string, mixed>
     */
    private function fetchNetworkData(Service $service): array
    {
        $ips = [];

        if ($service->ipv4_address) {
            $ips[] = [
                'address' => $service->ipv4_address,
                'type' => 'IPv4',
                'primary' => true,
            ];
        }

        if ($service->ipv6_address) {
            $ips[] = [
                'address' => $service->ipv6_address,
                'type' => 'IPv6',
                'primary' => false,
            ];
        }

        return [
            'ips' => $ips,
            'vlans' => [],
        ];
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    private function fetchTemplates(Service $service): array
    {
        // SynergyCP API: GET /api/v1/servers/{id}/templates
        return [];
    }
}
