<?php

namespace App\Http\Controllers;

use Inertia\Inertia;
use App\Domain\Tags\Models\Tag;
use Illuminate\Support\Facades\Request;
use Illuminate\Http\Request as HttpRequest;
use App\Domain\Feeds\Queries\FeedQueryBuilder;

class TagController extends Controller
{
    public function index($tag)
    {
        $tag = Tag::where('slug', $tag)->first();
        $filter = Request::get('filter');

        $feeds = (new FeedQueryBuilder())
            ->addTags([$tag->id])
            ->search(Request::only('search'))
            ->filter($filter)
            ->build();
        $nextCursor = $feeds->nextCursor;
        $feeds = $feeds->transformData();

        if(request()->wantsJson()){
            return ['collections' => $feeds, 'next' => $nextCursor];
        }

        $route = route('feed.tags', $tag->slug);

        return Inertia::render('Tag/Feed', [
            'searchFilters' => Request::all('search'),
            'feeds' => $feeds,
            'tag' => $tag,
            'local_route' => $route,
            'nextCursor' => $nextCursor,
            'active_filter' => $filter ?? 'new'
        ]);
    }

    public function listImagesTag($tag)
    {
        $tag = Tag::where('slug', $tag)->first();
        $filter = Request::get('filter');

        $feeds = (new FeedQueryBuilder())
            ->addTags([$tag->id])
            ->addType('image')
            ->search(Request::only('search'))
            ->filter($filter)
            ->build();

        $nextCursor = $feeds->nextCursor;
        $feeds = $feeds->transformData();

        if(request()->wantsJson()){
            return ['collections' => $feeds, 'next' => $nextCursor];
        }

        $route = route('list.images.tag', $tag->slug);

        return Inertia::render('Image/Index', [
            'nextCursor' => $nextCursor,
            'searchFilters' => Request::all('search'),
            'feeds' => $feeds,
            'tag' => $tag,
            'local_route' => $route,
            'active_filter' => $filter ?? 'new'
        ]);
    }


    public function listMusicsTag($tag)
    {
        $tag = Tag::where('slug', $tag)->first();
        $filter = Request::get('filter');

        $feeds = (new FeedQueryBuilder())
            ->addTags([$tag->id])
            ->addType('music')
            ->search(Request::only('search'))
            ->filter($filter)
            ->build();

        $nextCursor = $feeds->nextCursor;
        $feeds = $feeds->transformData();

        if(request()->wantsJson()){
            return ['collections' => $feeds, 'next' => $nextCursor];
        }

        $route = route('list.musics.tag', $tag->slug);

        return Inertia::render('Music/Feed', [
            'nextCursor' => $nextCursor,
            'searchFilters' => Request::all('search'),
            'feeds' => $feeds,
            'tag' => $tag,
            'local_route' => $route,
            'active_filter' => $filter ?? 'new'
        ]);
    }

    public function listVideosTag($tag)
    {
        $tag = Tag::where('slug', $tag)->first();
        $filter = Request::get('filter');

        $feeds = (new FeedQueryBuilder())
            ->addTags([$tag->id])
            ->addType('video')
            ->search(Request::only('search'))
            ->filter($filter)
            ->build();

        $nextCursor = $feeds->nextCursor;
        $feeds = $feeds->transformData();

        if(request()->wantsJson()){
            return ['collections' => $feeds, 'next' => $nextCursor];
        }

        $route = route('list.videos.tag', $tag->slug);

        return Inertia::render('Video/Feed', [
            'nextCursor' => $nextCursor,
            'searchFilters' => Request::all('search'),
            'feeds' => $feeds,
            'tag' => $tag,
            'local_route' => $route,
            'active_filter' => $filter ?? 'new'
        ]);
    }

}
