<?php

namespace App\Http\Controllers;

use App\Models\ImportMap;
use App\TaxonomyMap;
use App\Traits\ResolvesTaxonomyMapping;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ImportMapTaxonomyController extends Controller
{
    use ResolvesTaxonomyMapping;

    private function getProviderName(ImportMap $importMap): string
    {
        return tenant('id') . '-import-map-' . $importMap->id;
    }

    private function getLegacyProviderName(ImportMap $importMap): string
    {
        return 'import-map-' . $importMap->id;
    }

    public function index(Request $request, ImportMap $importMap)
    {
        $provider = $this->getProviderName($importMap);

        $query = TaxonomyMap::query()
            ->where('provider', $provider)
            ->when(
                $request->filled('term'),
                fn($query) => $query->where('term', 'like', '%' . $request->input('term') . '%')
            )
            ->when(
                $request->filled('taxonomy'),
                fn($query) => $query->where('taxonomy_type', $request->input('taxonomy'))
            )
            ->when(
                $request->input('status') === 'unmapped',
                fn($query) => $query->whereNull('taxonomy_id')
            )
            ->when(
                $request->input('status') === 'mapped',
                fn($query) => $query->whereNotNull('taxonomy_id')
            )
            ->orderBy('term');

        $types = TaxonomyMap::query()
            ->where('provider', $provider)
            ->pluck('taxonomy_type')
            ->unique()
            ->values()
            ->map(fn($type) => [
                'name' => __('taxonomies.' . $type),
                'slug' => $type,
            ]);

        $taxonomies = $query
            ->paginate(100)
            ->through(fn(TaxonomyMap $taxonomyMap) => [
                'id' => $taxonomyMap->id,
                'term' => $taxonomyMap->term,
                'taxonomy_type' => $taxonomyMap->taxonomy_type,
                'taxonomy_id' => $taxonomyMap->taxonomy_id,
                'parent_id' => $taxonomyMap->parent_id,
                'provider' => $taxonomyMap->provider,
            ])->toArray();

        $taxonomies['types'] = $types;

        return $this->getFilters($request->input('taxonomy'), $taxonomies);
    }

    public function store(Request $request, ImportMap $importMap): Response
    {
        $request->validate([
            'term' => 'required|string',
            'taxonomy_type' => 'required|string',
            'taxonomy_id' => 'required|integer',
        ]);

        $taxonomyMap = TaxonomyMap::create([
            'term' => $request->input('term'),
            'taxonomy_type' => $request->input('taxonomy_type'),
            'taxonomy_id' => $request->input('taxonomy_id'),
            'parent_id' => $request->input('parent_id'),
            'provider' => $this->getProviderName($importMap),
        ]);

        return response([
            'status' => 'ok',
            'data' => $taxonomyMap,
        ]);
    }

    public function update(Request $request, ImportMap $importMap, TaxonomyMap $taxonomyMap): Response
    {
        $request->validate([
            'taxonomy_id' => 'required|integer',
        ]);

        $taxonomyMap->update([
            'taxonomy_id' => $request->input('taxonomy_id'),
            'parent_id' => $request->input('parent_id', $taxonomyMap->parent_id),
        ]);

        $taxonomyMap->mapables()
            ->get()
            ->groupBy('tenant')
            ->each(fn($mapables, $tenant) => $this->resolveTaxonomyMapping($mapables, $tenant, $taxonomyMap));

        return response([
            'status' => 'ok',
            'data' => $taxonomyMap->fresh(),
        ]);
    }
}
