<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\VillageProfile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class VillageProfileController extends Controller
{
    public function edit()
    {
        $profile = VillageProfile::current();
        return view('admin.village-profile.edit', compact('profile'));
    }

    public function update(Request $request)
    {
        $validated = $request->validate([
            'nama_desa'             => 'required|string|max:255',
            'kecamatan'             => 'required|string|max:255',
            'kabupaten'             => 'required|string|max:255',
            'provinsi'              => 'required|string|max:255',
            'slogan'                => 'nullable|string|max:255',
            'sambutan_kepala_desa'  => 'nullable|string',
            'sejarah_singkat'       => 'nullable|string',
            'asal_usul'             => 'nullable|string',
            'visi'                  => 'nullable|string',
            'misi'                  => 'nullable|string',
            'motto'                 => 'nullable|string|max:255',
            'letak_geografis'       => 'nullable|string',
            'luas_wilayah'          => 'nullable|string|max:255',
            'ketinggian'            => 'nullable|string|max:255',
            'batas_wilayah'         => 'nullable|string',
            'kondisi_umum_wilayah'  => 'nullable|string',
            'foto_utama'            => 'nullable|image|mimes:jpg,jpeg,png,webp,gif|max:5120',
            'foto_kepala_desa'      => 'nullable|image|mimes:jpg,jpeg,png,webp,gif|max:5120',
        ]);

        $profile = VillageProfile::current();

        // Upload Foto Utama (Background Hero)
        if ($request->hasFile('foto_utama') && $request->file('foto_utama')->isValid()) {
            // Hapus file lama jika bukan URL external
            if ($profile->foto_utama && !str_starts_with($profile->foto_utama, 'http')) {
                Storage::disk('public')->delete($profile->foto_utama);
            }

            $file      = $request->file('foto_utama');
            $ext       = $file->getClientOriginalExtension() ?: 'jpg';
            $filename  = 'village_' . Str::random(12) . '.' . $ext;

            // Simpan langsung ke storage/app/public/village/
            $path = $file->storeAs('village', $filename, 'public');
            $validated['foto_utama'] = $path;
        } else {
            // Jangan overwrite jika tidak ada file baru
            unset($validated['foto_utama']);
        }

        // Upload Foto Kepala Desa
        if ($request->hasFile('foto_kepala_desa') && $request->file('foto_kepala_desa')->isValid()) {
            if ($profile->foto_kepala_desa && !str_starts_with($profile->foto_kepala_desa, 'http')) {
                Storage::disk('public')->delete($profile->foto_kepala_desa);
            }

            $file      = $request->file('foto_kepala_desa');
            $ext       = $file->getClientOriginalExtension() ?: 'jpg';
            $filename  = 'kades_' . Str::random(12) . '.' . $ext;
            $path      = $file->storeAs('officials', $filename, 'public');
            $validated['foto_kepala_desa'] = $path;
        } else {
            unset($validated['foto_kepala_desa']);
        }

        $profile->update($validated);

        return back()->with('success', 'Profil desa berhasil diperbarui! Foto akan tampil dalam beberapa saat.');
    }
}
