<?php

namespace App\Http\Controllers;

use App\Models\Dua;
use App\Models\DuaCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class DuaController extends Controller
{
    // Show all duas
    public function index()
    {
        $duas = Dua::with('category')->get();
        return view('duas.index', compact('duas'));
    }

    // Show the form for creating a new dua
    public function create()
    {
        $categories = DuaCategory::all();
        return view('duas.create', compact('categories'));
    }

    // Store a new dua
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'dua' => 'required|string',
            'translation' => 'required|string',
            'description' => 'nullable|string',
            'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', // Image, max 2MB
            'dua_category_id' => 'required|exists:dua_categories,id',
        ]);

        // Upload image if provided
        $imagePath = null;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('duas/images', 'public');
        }

        // Create dua
        Dua::create([
            'title' => $request->title,
            'dua' => $request->dua,
            'translation' => $request->translation,
            'description' => $request->description,
            'image' => $imagePath,
            'dua_category_id' => $request->dua_category_id,
        ]);

        return redirect()->route('duas.index')->with('success', 'Dua created successfully!');
    }

    // Show the form for editing a dua
    public function edit(Dua $dua)
    {
        $categories = DuaCategory::all();
        return view('duas.edit', compact('dua', 'categories'));
    }

    // Update a dua
    public function update(Request $request, Dua $dua)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'dua' => 'required|string',
            'translation' => 'required|string',
            'description' => 'nullable|string',
            'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', // Image, max 2MB
            'dua_category_id' => 'required|exists:dua_categories,id',
        ]);

        // Update image if provided
        if ($request->hasFile('image')) {
            // Delete old image
            if ($dua->image) {
                Storage::disk('public')->delete($dua->image);
            }
            // Upload new image
            $imagePath = $request->file('image')->store('duas/images', 'public');
            $dua->image = $imagePath;
        }

        // Update dua details
        $dua->update([
            'title' => $request->title,
            'dua' => $request->dua,
            'translation' => $request->translation,
            'description' => $request->description,
            'dua_category_id' => $request->dua_category_id,
        ]);

        return redirect()->route('duas.index')->with('success', 'Dua updated successfully!');
    }

    // Delete a dua
    public function destroy(Dua $dua)
    {
        // Delete image if exists
        if ($dua->image) {
            Storage::disk('public')->delete($dua->image);
        }
        // Delete dua
        $dua->delete();
        return redirect()->route('duas.index')->with('success', 'Dua deleted successfully!');
    }
}