<?php

namespace App\Http\Controllers\Admin\Settings;

use Illuminate\Http\Request;
use Inertia\Inertia;

class AppearanceSettingsController extends BaseSettingsController
{
    public function index()
    {
        $allSettings = $this->getAllSettings();

        return Inertia::render('admin/settings/appearance/index', [
            'heroBackgroundUrl' => $this->getBackgroundUrl('hero_background'),
            'communityBackgroundUrl' => $this->getBackgroundUrl('community_background'),
            'newsBackgroundUrl' => $this->getBackgroundUrl('news_background'),
            'downloadBackgroundUrl' => $this->getBackgroundUrl('download_background'),
            'loginBackgroundUrl' => $this->getBackgroundUrl('login_background'),
            'loginBackgroundPosition' => $this->setting('login_background_position', 'right'),
            'heroHeight' => (int) $this->setting('hero_height', 40),
            'heroUseLogo' => $this->getBoolSetting($allSettings, 'hero_use_logo', false),
            'heroLogoSize' => (int) $this->setting('hero_logo_size', 300),
            'smokeEnabled' => $this->getBoolSetting($allSettings, 'smoke_enabled', false),
            'newsSmokeEnabled' => $this->getBoolSetting($allSettings, 'news_smoke_enabled', true),
            'newsSparksEnabled' => $this->getBoolSetting($allSettings, 'news_sparks_enabled', true),
        ]);
    }

    public function update(Request $request)
    {
        $request->validate([
            'login_background' => 'nullable|file|mimes:jpeg,png,jpg,gif,svg,webp,mp4|max:5120',
            'hero_background' => 'nullable|file|mimes:jpeg,png,jpg,gif,svg,webp,mp4|max:51200',
            'community_background' => 'nullable|file|mimes:jpeg,png,jpg,gif,svg,webp|max:5120',
            'news_background' => 'nullable|file|mimes:jpeg,png,jpg,gif,svg,webp|max:5120',
            'download_background' => 'nullable|file|mimes:jpeg,png,jpg,gif,svg,webp|max:5120',
            'login_background_position' => 'required|string|in:left,right,full',
            'hero_height' => 'nullable|integer|min:20|max:100',
            'hero_use_logo' => 'required|boolean',
            'hero_logo_size' => 'required|integer|min:100|max:600',
            'smoke_enabled' => 'required|boolean',
            'news_smoke_enabled' => 'required|boolean',
            'news_sparks_enabled' => 'required|boolean',
        ]);

        $this->setSetting('login_background_position', $request->login_background_position);
        $this->setBoolSetting('hero_use_logo', $request->boolean('hero_use_logo'));
        $this->setSetting('hero_logo_size', $request->input('hero_logo_size', '300'));
        $this->setSetting('hero_height', $request->input('hero_height', '40'));
        $this->setBoolSetting('smoke_enabled', $request->boolean('smoke_enabled'));
        $this->setBoolSetting('news_smoke_enabled', $request->boolean('news_smoke_enabled'));
        $this->setBoolSetting('news_sparks_enabled', $request->boolean('news_sparks_enabled'));

        $this->handleBackgroundUpload($request, 'hero_background', 'hero');
        $this->handleBackgroundUpload($request, 'community_background', 'community');
        $this->handleBackgroundUpload($request, 'news_background', 'news');
        $this->handleBackgroundUpload($request, 'download_background', 'download');
        $this->handleBackgroundUpload($request, 'login_background', 'login');

        $this->refreshSettingsCache();

        return back()->with('success', 'Appearance settings updated successfully.');
    }

    private function getBackgroundUrl(string $key): ?string
    {
        $path = $this->setting($key);
        return $path ? asset($path) : null;
    }
}
