<?php

namespace App\Http\Controllers\Employee;

use App\Models\Weight;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\Employee\WeightRequest;
use Illuminate\View\View;

class WeightController extends Controller
{
    public function index(): View
    {
        // * Get all weights ordered by weight ascending
        $weights = Weight::orderBy('weight', 'asc')->get();

        return view('employee.pages.weight.index', compact('weights'));
    }

    public function create(): View
    {
        return view('employee.pages.weight.create');
    }

    public function store(WeightRequest $request): RedirectResponse
    {
        // * Create the weight
        Weight::create([
            'weight' => $request->weight,
        ]);

        // * Notification
        $notification = [
            'message' => 'Bobot telah berhasil ditambahkan',
            'alert-type' => 'info',
        ];

        return redirect()->route('weights')->with($notification);
    }

    public function edit(Weight $weights): View
    {
        return view('employee.pages.weight.edit', compact('weights'));
    }

    public function update(WeightRequest $request, Weight $weights): RedirectResponse
    {
        // * Update the weight
        $weights->update([
            'weight' => $request->weight,
        ]);

        // * Notification
        $notification = [
            'message' => 'Bobot telah berhasil diperbarui',
            'alert-type' => 'info',
        ];

        return redirect()->route('weights')->with($notification);
    }

    public function destroy(Weight $weights): RedirectResponse
    {
        // * Delete the weight
        $weights->delete();

        // * Notification
        $notification = [
            'message' => 'Bobot telah berhasil dihapus',
            'alert-type' => 'info',
        ];

        return redirect()->route('weights')->with($notification);
    }
}
