<?php

namespace App\Http\Controllers\Utils;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;

class CookieController extends Controller
{
  public function store(Request $request)
  {
    $request->validate([
      'key' => 'nullable',
      'value' => 'nullable',
      'days' => 'nullable',
    ]);

    $minutes = $request->days ? $request->days * 24 * 60 : 60;

    if ($request->filled('days')) {
      $minutes = $request->days * 24 * 60;
      $cookie = Cookie::make($request->key, $request->value, $minutes);
      Cookie::queue($cookie);
    } else {
      $cookie = Cookie::forever($request->key, $request->value);
      Cookie::queue($cookie);
    }

    return back();
  }

  public function destroy($key)
  {
    $key = urldecode($key);
    Cookie::queue(Cookie::forget($key));
    return back();
  }
}
