<?php

namespace App\Http\Controllers;

use App\Models\InformasiUmum;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

class InformasiUmumController extends Controller
{
    protected function storagePath()
    {
        return env('STORAGE_PATH', public_path('penyimpanan'));
    }

    public function index()
    {
        $data = InformasiUmum::orderBy('id_informasi_umum', 'desc')->paginate(15);
        return view('admin.informasi-umum', compact('data'));
    }

    public function create()
    {
        return view('admin.form-informasi-umum');
    }

    public function store(Request $request)
    {
        $request->validate([
            'visi' => 'nullable|string',
            'misi' => 'nullable|string',
            'sejarah' => 'nullable|string',
            'foto_struktur' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
        ]);

        $data = $request->all();
        $data['id_user'] = Auth::id();
        
        $storagePath = $this->storagePath();

        if ($request->hasFile('foto_struktur')) {
            if (!file_exists($storagePath . '/foto_informasi_umum')) {
                mkdir($storagePath . '/foto_informasi_umum', 0755, true);
            }
            $filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto_struktur')->getClientOriginalName());
            $request->file('foto_struktur')->move($storagePath . '/foto_informasi_umum', $filename);
            $data['foto_struktur'] = 'foto_informasi_umum/' . $filename;
        }

        InformasiUmum::create($data);

        return redirect()->route('informasi-umum.index')->with('success', 'Data berhasil ditambahkan');
    }

    public function edit($id)
    {
        $informasiUmum = InformasiUmum::findOrFail($id);
        return view('admin.edit-informasi-umum', compact('informasiUmum'));
    }

    public function update(Request $request, $id)
    {
        $informasiUmum = InformasiUmum::findOrFail($id);

        $request->validate([
            'visi' => 'nullable|string',
            'misi' => 'nullable|string',
            'sejarah' => 'nullable|string',
            'foto_struktur' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
        ]);

        $data = $request->all();
        $storagePath = $this->storagePath();

        if ($request->hasFile('foto_struktur')) {
            // Delete old photo if exists
            if ($informasiUmum->foto_struktur && file_exists($storagePath . '/' . $informasiUmum->foto_struktur)) {
                unlink($storagePath . '/' . $informasiUmum->foto_struktur);
            }
            
            if (!file_exists($storagePath . '/foto_informasi_umum')) {
                mkdir($storagePath . '/foto_informasi_umum', 0755, true);
            }
            $filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto_struktur')->getClientOriginalName());
            $request->file('foto_struktur')->move($storagePath . '/foto_informasi_umum', $filename);
            $data['foto_struktur'] = 'foto_informasi_umum/' . $filename;
        } else {
            unset($data['foto_struktur']);
        }

        $informasiUmum->update($data);

        return redirect()->route('informasi-umum.index')->with('success', 'Data berhasil diupdate');
    }

    public function destroy($id)
    {
        $informasiUmum = InformasiUmum::findOrFail($id);
        
        $storagePath = $this->storagePath();
        
        if ($informasiUmum->foto_struktur && file_exists($storagePath . '/' . $informasiUmum->foto_struktur)) {
            unlink($storagePath . '/' . $informasiUmum->foto_struktur);
        }
        
        $informasiUmum->delete();
        return redirect()->route('informasi-umum.index')->with('success', 'Data berhasil dihapus');
    }

    public function visiMisi()
    {
        $data = InformasiUmum::first();
        return view('user.visi-misi', compact('data'));
    }

    public function sejarahSekolah()
    {
        $data = InformasiUmum::first();
        return view('user.sejarah-sekolah', compact('data'));
    }

    public function strukturOrganisasi()
    {
        $data = InformasiUmum::first();
        return view('user.struktur-organisasi', compact('data'));
    }
}
