<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\GlobalCategory;
use App\Models\Tenants\Tenant;
use App\Models\Tenants\TenantSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class CategoryController extends Controller
{
    /**
     * 커스텀 그룹 라벨 조회
     */
    private function getCustomGroupLabels(): array
    {
        $tenantId = session('selected_tenant_id');
        if (! $tenantId) {
            return [];
        }

        $setting = TenantSetting::withoutGlobalScopes()
            ->where('tenant_id', $tenantId)
            ->where('setting_group', 'category')
            ->where('setting_key', 'custom_group_labels')
            ->first();

        return $setting?->setting_value ?? [];
    }

    /**
     * 카테고리 그룹 추가
     */
    public function storeGroup(Request $request): RedirectResponse
    {
        $tenantId = session('selected_tenant_id');
        if (! $tenantId) {
            return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
        }

        $validated = $request->validate([
            'group_code' => ['required', 'string', 'max:50', 'regex:/^[a-z][a-z0-9_]*$/'],
            'group_label' => 'required|string|max:50',
        ], [
            'group_code.regex' => '그룹 코드는 영문 소문자, 숫자, 언더스코어만 사용 가능합니다.',
        ]);

        $groupCode = $validated['group_code'];
        $groupLabel = $validated['group_label'];

        // DB에 이미 존재하는 그룹인지 체크
        $existsInGlobal = GlobalCategory::whereNull('deleted_at')
            ->where('code_group', $groupCode)
            ->exists();
        $existsInTenant = Category::where('code_group', $groupCode)->exists();

        if ($existsInGlobal || $existsInTenant) {
            return redirect()->back()->with('error', '이미 존재하는 그룹 코드입니다.');
        }

        // 커스텀 라벨 중복 체크
        $customLabels = $this->getCustomGroupLabels();
        if (isset($customLabels[$groupCode])) {
            return redirect()->back()->with('error', '이미 존재하는 커스텀 그룹 코드입니다.');
        }

        $customLabels[$groupCode] = $groupLabel;

        TenantSetting::withoutGlobalScopes()->updateOrCreate(
            [
                'tenant_id' => $tenantId,
                'setting_group' => 'category',
                'setting_key' => 'custom_group_labels',
            ],
            [
                'setting_value' => $customLabels,
                'description' => '카테고리 커스텀 그룹 라벨',
            ]
        );

        return redirect()
            ->route('categories.index', ['group' => $groupCode])
            ->with('success', "'{$groupLabel}' 그룹이 추가되었습니다.");
    }

    /**
     * 카테고리 관리 페이지
     */
    public function index(Request $request): View|Response
    {
        // HTMX 요청 시 전체 페이지 리로드
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('categories.index'));
        }

        $tenantId = session('selected_tenant_id');
        $tenant = $tenantId ? Tenant::find($tenantId) : null;
        $isHQ = $tenantId == 1;

        // 글로벌 그룹 + description
        $globalGroupDescs = GlobalCategory::whereNull('deleted_at')
            ->selectRaw('code_group, MIN(description) as description')
            ->groupBy('code_group')
            ->pluck('description', 'code_group')
            ->toArray();

        // 테넌트 그룹 + description
        $tenantGroupDescs = [];
        if ($tenantId) {
            $tenantGroupDescs = Category::where('tenant_id', $tenantId)
                ->whereNull('deleted_at')
                ->selectRaw('code_group, MIN(description) as description')
                ->groupBy('code_group')
                ->pluck('description', 'code_group')
                ->toArray();
        }

        // 커스텀 그룹 라벨 (빈 그룹용 — TenantSetting)
        $customLabels = $this->getCustomGroupLabels();

        // config 라벨 (하위호환 폴백)
        $configLabels = config('categories.code_group_labels', []);

        // 그룹별 스코프 분류
        $allGroupKeys = array_unique(array_merge(
            array_keys($globalGroupDescs),
            array_keys($tenantGroupDescs),
            array_keys($customLabels)
        ));
        sort($allGroupKeys);

        $codeGroups = [];
        $groupScopes = [];
        foreach ($allGroupKeys as $group) {
            $inGlobal = array_key_exists($group, $globalGroupDescs);
            $inTenant = array_key_exists($group, $tenantGroupDescs);

            $codeGroups[$group] = ! empty($globalGroupDescs[$group])
                ? $globalGroupDescs[$group]
                : (! empty($tenantGroupDescs[$group])
                    ? $tenantGroupDescs[$group]
                    : ($customLabels[$group] ?? $configLabels[$group] ?? $group));

            if ($inGlobal && $inTenant) {
                $groupScopes[$group] = 'both';
            } elseif ($inGlobal) {
                $groupScopes[$group] = 'global';
            } elseif ($inTenant) {
                $groupScopes[$group] = 'tenant';
            } else {
                $groupScopes[$group] = 'custom';
            }
        }

        if (empty($codeGroups)) {
            $codeGroups['product'] = $configLabels['product'] ?? 'product';
            $groupScopes['product'] = 'custom';
        }

        $selectedGroup = $request->get('group', array_key_first($codeGroups));
        if (! isset($codeGroups[$selectedGroup])) {
            $selectedGroup = array_key_first($codeGroups);
        }

        // 글로벌 카테고리 (트리 구조로 평탄화)
        $globalCategoriesRaw = GlobalCategory::where('code_group', $selectedGroup)
            ->whereNull('deleted_at')
            ->orderBy('sort_order')
            ->get();
        $globalCategories = $this->flattenTree($globalCategoriesRaw);

        // 테넌트 카테고리 (트리 구조로 평탄화)
        $tenantCategories = collect();
        $tenantCodes = [];
        if ($tenantId) {
            $tenantCategoriesRaw = Category::where('tenant_id', $tenantId)
                ->where('code_group', $selectedGroup)
                ->whereNull('deleted_at')
                ->orderBy('sort_order')
                ->get();
            $tenantCategories = $this->flattenTree($tenantCategoriesRaw);
            $tenantCodes = $tenantCategoriesRaw->pluck('code')->toArray();
        }

        $globalCodes = $globalCategoriesRaw->pluck('code')->toArray();

        return view('categories.index', [
            'codeGroups' => $codeGroups,
            'groupScopes' => $groupScopes,
            'codeGroupLabels' => $codeGroups,
            'selectedGroup' => $selectedGroup,
            'globalCategories' => $globalCategories,
            'tenantCategories' => $tenantCategories,
            'tenantCodes' => $tenantCodes,
            'globalCodes' => $globalCodes,
            'tenant' => $tenant,
            'isHQ' => $isHQ,
        ]);
    }

    /**
     * 카테고리 컬렉션을 트리 구조로 평탄화 (부모-자식 순서 유지, depth 추가)
     */
    private function flattenTree($categories): \Illuminate\Support\Collection
    {
        $result = collect();
        $byParent = $categories->groupBy(fn ($c) => $c->parent_id ?? 0);

        $this->addChildrenRecursive($result, $byParent, 0, 0);

        return $result;
    }

    /**
     * 재귀적으로 자식 추가
     */
    private function addChildrenRecursive(&$result, $byParent, $parentId, $depth): void
    {
        $children = $byParent->get($parentId, collect());

        foreach ($children as $child) {
            $child->depth = $depth;
            $result->push($child);
            $this->addChildrenRecursive($result, $byParent, $child->id, $depth + 1);
        }
    }
}
