<?php

namespace App\Livewire;

use App\Models\Watched as UserWatched;
use Illuminate\Support\Carbon;
use Livewire\Component;
use Livewire\WithPagination;

class Watched extends Component
{
    use WithPagination;

    public $userId;

    public function mount($user)
    {
        $this->userId = $user ? $user->id : auth()->user()->id;
    }

    public function render()
    {
        $watched = UserWatched::where('user_id', $this->userId)->orderBy('created_at', 'desc')->paginate(25);
        $watchedGrouped = $watched->groupBy(function ($val) {
            return Carbon::parse($val->created_at)->format('h');
        });

        return view('livewire.watched', [
            'watched' => $watched,
            'watchedGrouped' => $watchedGrouped,
        ]);
    }
}
