<?php

namespace App\Http\Controllers;

use App\Http\Resources\Models\ItemProtoResource;
use App\Http\Resources\Models\ItemProtoSearchResource;
use App\Http\Resources\Models\ItemProtoShopResource;
use App\Models\Game\Player\ItemProto;
use Illuminate\Http\Request;

class WikiController extends Controller
{
    public $itemColumns = [
        'vnum',
        'locale_name',
        'type',
        'subtype',
        'refined_vnum',
        'refine_set',
        'magic_pct',
        // Limits
        'limittype0',
        'limitvalue0',
        'limittype1',
        'limitvalue1',
        // Applies
        'applytype0',
        'applyvalue0',
        'applytype1',
        'applyvalue1',
        'applytype2',
        'applyvalue2',
    ];

    public function search(Request $request)
    {
        return ItemProtoSearchResource::collection(
            ItemProto::query()
                ->select('locale_name', 'vnum')
                ->when($request->query('query'), fn ($q, $term) => $q->where('locale_name', 'like', "%$term%"))
                ->take(10)
                ->get()
        );
    }

    public function show(ItemProto $item)
    {
        if ($item->type->isUpgradeable()) {
            $baseName = str($item->locale_name)
                ->beforeLast('+');

            $items = ItemProto::select($this->itemColumns)
                ->where('locale_name', 'like', "$baseName%")
                ->orderBy('vnum', 'asc')
                ->with([
                    'refineSet',
                    'refineSet.item0' => fn ($q) => $q->select(['vnum', 'locale_name']),
                    'refineSet.item1' => fn ($q) => $q->select(['vnum', 'locale_name']),
                    'refineSet.item2' => fn ($q) => $q->select(['vnum', 'locale_name']),
                    'refineSet.item3' => fn ($q) => $q->select(['vnum', 'locale_name']),
                    'refineSet.item4' => fn ($q) => $q->select(['vnum', 'locale_name']),
                    'sellers.mob',
                ])
                ->get();

            if ($items->first()->vnum) {
                $from = ItemProto::select($this->itemColumns)
                    ->whereRefinedVnum($items->first()->vnum)
                    // ->where('refine_set', '!=', 0)
                    ->first();

                if ($from) {
                    $items->prepend($from);
                }
            }

            if (($last = $items->last())->refined_vnum && $last->refine_set !== 0) {
                $to = ItemProto::select($this->itemColumns)
                    ->whereVnum($items->last()->refined_vnum)
                    ->first();

                if ($to) {
                    $items->push($to);
                }
            }

            return [
                'items' => ItemProtoResource::collection($items),
                'sources' => [
                    'shops' => ItemProtoShopResource::collection(
                        $items
                            ->flatMap(fn ($item) => $item->sellers)
                            ->unique(fn ($seller) => $seller->vnum)
                    ),
                ],
            ];
        }

        return new ItemProtoResource(
            $item->loadMissing([
                'sellers.mob',
            ])
        );
    }
}
