<?php

namespace App\Http\Controllers;

use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class SettingController extends Controller
{
    /**
     * Return current branding settings.
     */
    public function index()
    {
        return response()->json([
            'logo_url' => Setting::logoUrl(),
            'favicon_url' => Setting::faviconUrl(),
        ]);
    }

    /**
     * Upload logo (PNG/JPG/WebP/SVG).
     */
    public function uploadLogo(Request $request)
    {
        $request->validate([
            'image' => ['required', 'image', 'mimes:jpg,jpeg,png,webp,svg', 'max:1024'], // 1 MB
        ]);

        $this->deleteFile(Setting::get('logo_path'));
        $path = $request->file('image')->store('branding', 'public');
        Setting::set('logo_path', $path);

        return $this->index();
    }

    /**
     * Upload favicon (PNG/ICO/SVG, square sebaiknya).
     */
    public function uploadFavicon(Request $request)
    {
        $request->validate([
            'image' => ['required', 'file', 'mimes:png,ico,svg,webp', 'max:512'], // 512 KB
        ]);

        $this->deleteFile(Setting::get('favicon_path'));
        $path = $request->file('image')->store('branding', 'public');
        Setting::set('favicon_path', $path);

        return $this->index();
    }

    public function deleteLogo()
    {
        $this->deleteFile(Setting::get('logo_path'));
        Setting::set('logo_path', null);
        return $this->index();
    }

    public function deleteFavicon()
    {
        $this->deleteFile(Setting::get('favicon_path'));
        Setting::set('favicon_path', null);
        return $this->index();
    }

    private function deleteFile(?string $path): void
    {
        if ($path && Storage::disk('public')->exists($path)) {
            Storage::disk('public')->delete($path);
        }
    }

    /**
     * Get mitra (OKE Laundry) configuration.
     */
    public function mitra()
    {
        return response()->json(Setting::mitra());
    }

    /**
     * Update mitra configuration.
     */
    public function updateMitra(Request $request)
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'address' => ['nullable', 'string', 'max:500'],
            'phone' => ['nullable', 'string', 'max:30', 'regex:/^(\+62|62|0)8[1-9][0-9]{7,11}$/'],
            'hours' => ['nullable', 'string', 'max:255'],
            'lat' => ['required', 'numeric', 'between:-90,90'],
            'lng' => ['required', 'numeric', 'between:-180,180'],
            'cost_per_km' => ['required', 'integer', 'min:0', 'max:1000000'],
            'free_radius_km' => ['required', 'numeric', 'min:0', 'max:100'],
        ], [
            'phone.regex' => 'Format nomor HP tidak valid (contoh: 081234567890).',
        ]);

        Setting::set('mitra_name', $validated['name']);
        Setting::set('mitra_address', $validated['address'] ?? '');
        Setting::set('mitra_phone', $validated['phone'] ?? '');
        Setting::set('mitra_hours', $validated['hours'] ?? '');
        Setting::set('mitra_lat', (string) $validated['lat']);
        Setting::set('mitra_lng', (string) $validated['lng']);
        Setting::set('mitra_cost_per_km', (string) $validated['cost_per_km']);
        Setting::set('mitra_free_radius_km', (string) $validated['free_radius_km']);

        return $this->mitra();
    }
}
