<?php

namespace App\Http\Controllers;

use App\Http\Requests\EntryRequest;
use App\Models\Entry;
use App\Models\Habit;
use Illuminate\Http\Request;

class EntryController extends Controller
{
    public function index()
    {

    }

    public function create(Habit $habit)
    {
        return view('entries.create', [
            'habit' => $habit
        ]);
    }

    public function store(EntryRequest $request)
    {
        $habit = Habit::find($request->input('habit-id'));

        // If value doesn't exist (checkbox not checked), set it to '0', else '1'.
        if (is_null($request->input('value'))) {
            $value = 0;
        } elseif ($request->input('value') === "ON") {
            $value = 1;
        } else {
            $value = $request->input('value');
        }

        Entry::create([
            'habit_id' => $habit->id,
            'value' => $value,
            'date' => $request->input('input-date'),
            'note' => $request->input('note'),
        ]);

        return redirect()->route('habits.show', $habit)->with('success');
    }

    public function show($id)
    {
    }

    public function edit($id)
    {
    }

    public function update(Request $request, $id)
    {
    }

    public function destroy($id)
    {
    }
}
