<?php
namespace App\Http\Controllers\V1\User;

use App\Http\Controllers\V1\Controller;
use App\Models\UserConsumeRecord;
use App\Models\UserEpisodesRecord;
use App\Models\UserWatchRecord;
use Carbon\Carbon;
use Dingo\Api\Http\Request;
use Illuminate\Database\Eloquent\Builder;

class ConsumeController extends Controller
{
    public function record(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * 10;
        $date = $request->input('date');
        $date= $date??Carbon::now()->format('Y-m');
        $lists  = UserEpisodesRecord::filterUser()
            ->when($date,function ($query,$date){
                /* @var Builder  $query*/
                return $query->where('created_at', 'like', "$date%");
            })
            ->with(['detail.episode'])
            ->orderByDesc('id')
            ->limit($limit)
            ->offset($offset)
            ->get();

        foreach ($lists as $list){
            $count = $list->detail->episode->withCount('lists')->first()->toArray();
            $list->detail->episode->total = $count['lists_count'];
            $list->detail->episode->status_text = $list->detail->episode->status;
        }

        return $this->success($lists);
    }
}
