<?php

namespace App\Http\Controllers;

use App\Enums\FoodCategory;
use App\Enums\WeightUnit;
use App\Http\Requests\RefrigeratorItemRequest;
use App\Models\RefrigeratorItem;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class RefrigeratorController extends Controller
{
    public function index(Request $request): Response
    {
        $query = $request->user()->refrigeratorItems()->latest();

        if ($request->filled('search')) {
            $query->where('name', 'like', '%'.$request->string('search').'%');
        }

        if ($request->filled('category')) {
            $query->where('category', $request->string('category'));
        }

        $items = $query->get()->map(fn (RefrigeratorItem $item) => [
            'id' => $item->id,
            'name' => $item->name,
            'category' => $item->category->value,
            'category_label' => $item->category->label(),
            'quantity' => $item->quantity,
            'unit' => $item->unit->value,
            'unit_label' => $item->unit->label(),
            'expiry_date' => $item->expiry_date?->toDateString(),
            'added_at' => $item->added_at?->toDateTimeString(),
            'is_expiring_soon' => $item->expiry_date?->isBetween(now()->startOfDay(), now()->addDays(3)->endOfDay()) ?? false,
            'is_expired' => $item->expiry_date?->isPast() ?? false,
        ]);

        return Inertia::render('Refrigerator/Index', [
            'items' => $items,
            'filters' => [
                'search' => $request->string('search')->toString(),
                'category' => $request->string('category')->toString(),
            ],
            'categories' => collect(FoodCategory::cases())->map(fn (FoodCategory $c) => [
                'value' => $c->value,
                'label' => $c->label(),
            ]),
            'units' => collect(WeightUnit::cases())->map(fn (WeightUnit $u) => [
                'value' => $u->value,
                'label' => $u->label(),
            ]),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('Refrigerator/Create', [
            'categories' => collect(FoodCategory::cases())->map(fn (FoodCategory $c) => [
                'value' => $c->value,
                'label' => $c->label(),
            ]),
            'units' => collect(WeightUnit::cases())->map(fn (WeightUnit $u) => [
                'value' => $u->value,
                'label' => $u->label(),
            ]),
        ]);
    }

    public function store(RefrigeratorItemRequest $request): RedirectResponse
    {
        $item = new RefrigeratorItem($request->validated());
        $item->user_id = $request->user()->id;
        $item->weight_grams = $item->getWeightInGrams();
        $item->save();

        Inertia::flash('toast', ['type' => 'success', 'message' => __('refrigerator.item_added')]);

        return to_route('refrigerator.index');
    }

    public function edit(Request $request, RefrigeratorItem $refrigeratorItem): Response
    {
        abort_unless($refrigeratorItem->user_id === $request->user()->id, 403);

        return Inertia::render('Refrigerator/Edit', [
            'item' => [
                'id' => $refrigeratorItem->id,
                'name' => $refrigeratorItem->name,
                'category' => $refrigeratorItem->category->value,
                'quantity' => $refrigeratorItem->quantity,
                'unit' => $refrigeratorItem->unit->value,
                'expiry_date' => $refrigeratorItem->expiry_date?->toDateString(),
                'added_at' => $refrigeratorItem->added_at?->toDateTimeString(),
            ],
            'categories' => collect(FoodCategory::cases())->map(fn (FoodCategory $c) => [
                'value' => $c->value,
                'label' => $c->label(),
            ]),
            'units' => collect(WeightUnit::cases())->map(fn (WeightUnit $u) => [
                'value' => $u->value,
                'label' => $u->label(),
            ]),
        ]);
    }

    public function update(RefrigeratorItemRequest $request, RefrigeratorItem $refrigeratorItem): RedirectResponse
    {
        abort_unless($refrigeratorItem->user_id === $request->user()->id, 403);

        $refrigeratorItem->fill($request->validated());
        $refrigeratorItem->weight_grams = $refrigeratorItem->getWeightInGrams();
        $refrigeratorItem->save();

        Inertia::flash('toast', ['type' => 'success', 'message' => __('refrigerator.item_updated')]);

        return to_route('refrigerator.index');
    }

    public function destroy(Request $request, RefrigeratorItem $refrigeratorItem): RedirectResponse
    {
        abort_unless($refrigeratorItem->user_id === $request->user()->id, 403);

        $refrigeratorItem->delete();

        Inertia::flash('toast', ['type' => 'success', 'message' => __('refrigerator.item_deleted')]);

        return to_route('refrigerator.index');
    }
}
