<?php

namespace App\Http\Controllers;

use App\Support\AppSettings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class SettingsController extends Controller
{
    public function index()
    {
        $settings = DB::table('app_settings')
            ->whereNull('branch_id')
            ->pluck('value', 'key')
            ->map(fn ($value) => json_decode($value, true) ?? $value);

        return view('settings.index', [
            'settings' => $settings,
            'logoUrl' => AppSettings::logoUrl(),
            'landingHeroUrl' => '/storage/'.ltrim(AppSettings::get('landing_hero_image', 'landing/wash-wear-hero.png'), '/'),
            'landingBeforeAfterUrl' => '/storage/'.ltrim(AppSettings::get('landing_before_after_image', 'landing/wash-wear-before-after.png'), '/'),
            'landingCareKitUrl' => '/storage/'.ltrim(AppSettings::get('landing_care_kit_image', 'landing/wash-wear-care-kit.png'), '/'),
        ]);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'settings' => ['required', 'array'],
            'logo' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp,svg', 'max:2048'],
            'landing_hero' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:4096'],
            'landing_before_after' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:4096'],
            'landing_care_kit' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:4096'],
        ]);

        if ($request->hasFile('logo')) {
            $oldLogo = AppSettings::get('business_logo');
            if ($oldLogo) {
                Storage::disk('public')->delete($oldLogo);
            }

            $data['settings']['business_logo'] = $request->file('logo')->store('branding', 'public');
        }

        if ($request->hasFile('landing_hero')) {
            $oldHero = AppSettings::get('landing_hero_image');
            if ($oldHero && $oldHero !== 'landing/wash-wear-hero.png') {
                Storage::disk('public')->delete($oldHero);
            }

            $data['settings']['landing_hero_image'] = $request->file('landing_hero')->store('landing', 'public');
        }

        if ($request->hasFile('landing_before_after')) {
            $oldBeforeAfter = AppSettings::get('landing_before_after_image');
            if ($oldBeforeAfter && $oldBeforeAfter !== 'landing/wash-wear-before-after.png') {
                Storage::disk('public')->delete($oldBeforeAfter);
            }

            $data['settings']['landing_before_after_image'] = $request->file('landing_before_after')->store('landing', 'public');
        }

        if ($request->hasFile('landing_care_kit')) {
            $oldCareKit = AppSettings::get('landing_care_kit_image');
            if ($oldCareKit && $oldCareKit !== 'landing/wash-wear-care-kit.png') {
                Storage::disk('public')->delete($oldCareKit);
            }

            $data['settings']['landing_care_kit_image'] = $request->file('landing_care_kit')->store('landing', 'public');
        }

        foreach ($data['settings'] as $key => $value) {
            DB::table('app_settings')->updateOrInsert(
                ['branch_id' => null, 'key' => $key],
                ['value' => json_encode($value), 'updated_at' => now(), 'created_at' => now()],
            );
        }

        AppSettings::clear();

        return back()->with('status', 'Pengaturan disimpan.');
    }
}
