<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Sampah;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class SampahController extends Controller
{
    public function index()
    {
        $title = 'Data Sampah';

        $sampah = Sampah::with('user')->orderBy('tahun', 'desc')->get();

        return view('admin.sampah.index', compact('title', 'sampah'));
    }

    public function create()
    {
        $title = 'Tambah Data Sampah';
        $users = User::all();

        return view('admin.sampah.create', compact('title', 'users'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'tahun'             => 'required|numeric',
            'total_sampah'      => 'required|numeric',
            'total_kelola'      => 'required|numeric',
            'total_daur_ulang'  => 'required|numeric',
        ]);

        $sisa_sampah = $request->total_sampah
            - ($request->total_kelola + $request->total_daur_ulang);

        Sampah::create([
            'user_id'           => Auth::id(),
            'tahun'             => $request->tahun,
            'total_sampah'      => $request->total_sampah,
            'total_kelola'      => $request->total_kelola,
            'total_daur_ulang'  => $request->total_daur_ulang,
            'sisa_sampah'       => $sisa_sampah,
        ]);

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

    public function edit($id)
    {
        $title = 'Edit Data Sampah';
        $sampah = Sampah::findOrFail($id);
        $users = User::all();

        return view('admin.sampah.edit', compact('title', 'sampah', 'users'));
    }

    public function update(Request $request, $id)
    {
        $sampah = Sampah::findOrFail($id);

        $request->validate([
            'user_id'           => 'required|exists:users,id',
            'tahun'             => 'required|numeric',
            'total_sampah'      => 'required|numeric',
            'total_kelola'      => 'required|numeric',
            'total_daur_ulang'  => 'required|numeric',
        ]);

        $sisa_sampah = $request->total_sampah
            - ($request->total_kelola + $request->total_daur_ulang);

        $sampah->update([
            'user_id'           => $request->user_id,
            'tahun'             => $request->tahun,
            'total_sampah'      => $request->total_sampah,
            'total_kelola'      => $request->total_kelola,
            'total_daur_ulang'  => $request->total_daur_ulang,
            'sisa_sampah'       => $sisa_sampah,
        ]);

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

    public function destroy($id)
    {
        $sampah = Sampah::findOrFail($id);
        $sampah->delete();

        return redirect()->route('admin.sampah.index')
            ->with('success', 'Data sampah berhasil dihapus');
    }
}
