<?php

namespace App\Http\Controllers;

use App\Models\Buku;
use App\Models\Kategori;
use Illuminate\Http\Request;

class AdminBukuController extends Controller
{
    public function create()
    {
        $kategori = Kategori::all();
        return view('admin.buku.create', compact('kategori'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'bibid' => 'required|string|max:30|unique:buku,bibid',
            'judul' => 'required|string',
            'pengarang' => 'required|string|max:100',
            'penerbit' => 'nullable|string',
            'tahun_terbit' => 'nullable|digits:4',
            'edisi' => 'nullable|string|max:50',
            'deskripsi_fisik' => 'nullable|string|max:100',
            'nomor_panggil' => 'required|string|max:50',
            'eksemplar' => 'required|integer|min:1',
            'id_kategori' => 'required|exists:kategori,id_kategori',
            'cover' => 'image|mimes:jpeg,png,jpg,webp|max:2048',
        ]);

        if ($request->hasFile('cover')) {
            $validated['cover'] = $request->file('cover')->store('covers', 'public');
        }

        $validated['konten_digital'] = 0;
        Buku::create($validated);

        return back()->with('success', 'Aset buku baru berhasil ditambahkan.');
    }

    public function index(Request $request)
    {
        $search = $request->input('search');
        $buku = Buku::with('kategori')
            ->when($search, function ($query, $search) {
                return $query->where('judul', 'like', "%{$search}%")
                    ->orWhere('bibid', 'like', "%{$search}%");
            })
            ->paginate(15);

        $kategori = Kategori::all();

        return view('admin.buku.index', compact('buku', 'search', 'kategori'));
    }

    public function edit($id)
    {
        $buku = Buku::findOrFail($id);
        $kategori = Kategori::all();

        return view('admin.buku.edit', compact('buku', 'kategori'));
    }

    public function update(Request $request, $id)
    {
        $buku = Buku::findOrFail($id);

        $validated = $request->validate([
            'bibid' => 'required|string|max:30|unique:buku,bibid,' . $id . ',id_buku',
            'judul' => 'required|string',
            'pengarang' => 'required|string|max:100',
            'penerbit' => 'nullable|string',
            'tahun_terbit' => 'nullable|digits:4',
            'edisi' => 'nullable|string|max:50',
            'deskripsi_fisik' => 'nullable|string|max:100',
            'nomor_panggil' => 'required|string|max:50',
            'eksemplar' => 'required|integer|min:1',
            'id_kategori' => 'required|exists:kategori,id_kategori',
            'cover' => 'image|mimes:jpeg,png,jpg,webp|max:2048'
        ]);

        if ($request->hasFile('cover')) {
            if ($buku->cover && \Illuminate\Support\Facades\Storage::disk('public')->exists($buku->cover)) {
                \Illuminate\Support\Facades\Storage::disk('public')->delete($buku->cover);
            }
            $validated['cover'] = $request->file('cover')->store('covers', 'public');
        }

        $buku->update($validated);

        return redirect()->route('admin.buku.index')->with('success', 'Aset buku berhasil diperbarui.');
    }

    public function destroy($id)
    {
        $buku = Buku::findOrFail($id);
        $buku->delete();

        return redirect()->route('admin.buku.index')->with('success', 'Aset buku berhasil dihapus dari sistem.');
    }
}
