<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\GlobalCategory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class GlobalCategoryApiController extends Controller
{
    /**
     * 목록 조회 (드롭다운용)
     */
    public function list(Request $request): JsonResponse
    {
        $codeGroup = $request->input('code_group', 'product');

        $categories = GlobalCategory::query()
            ->where('code_group', $codeGroup)
            ->whereNull('deleted_at')
            ->orderBy('parent_id')
            ->orderBy('sort_order')
            ->get(['id', 'parent_id', 'code', 'name']);

        return response()->json([
            'success' => true,
            'data' => $categories,
        ]);
    }

    /**
     * 단일 조회
     */
    public function show(int $id): JsonResponse
    {
        $category = GlobalCategory::findOrFail($id);

        return response()->json([
            'success' => true,
            'data' => $category,
        ]);
    }

    /**
     * 생성
     */
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'code_group' => 'required|string|max:30',
            'parent_id' => 'nullable|integer|exists:global_categories,id',
            'code' => 'required|string|max:30',
            'name' => 'required|string|max:100',
            'profile_code' => 'nullable|string|max:30',
            'description' => 'nullable|string|max:255',
            'sort_order' => 'nullable|integer|min:0',
        ]);

        // 코드 중복 체크
        $exists = GlobalCategory::query()
            ->where('code_group', $validated['code_group'])
            ->where('code', $validated['code'])
            ->whereNull('deleted_at')
            ->exists();

        if ($exists) {
            return response()->json(['success' => false, 'message' => '이미 존재하는 코드입니다.'], 400);
        }

        // 최대 sort_order 조회
        $maxSortOrder = GlobalCategory::query()
            ->where('code_group', $validated['code_group'])
            ->where('parent_id', $validated['parent_id'] ?? null)
            ->max('sort_order') ?? 0;

        $category = GlobalCategory::create([
            'parent_id' => $validated['parent_id'] ?? null,
            'code_group' => $validated['code_group'],
            'code' => $validated['code'],
            'name' => $validated['name'],
            'profile_code' => $validated['profile_code'] ?? null,
            'description' => $validated['description'] ?? null,
            'is_active' => true,
            'sort_order' => $validated['sort_order'] ?? ($maxSortOrder + 1),
            'created_by' => Auth::id(),
        ]);

        return response()->json([
            'success' => true,
            'message' => '글로벌 카테고리가 생성되었습니다.',
            'data' => $category,
        ]);
    }

    /**
     * 수정
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $category = GlobalCategory::findOrFail($id);

        $validated = $request->validate([
            'name' => 'sometimes|required|string|max:100',
            'parent_id' => 'nullable|integer|exists:global_categories,id',
            'profile_code' => 'nullable|string|max:30',
            'description' => 'nullable|string|max:255',
            'sort_order' => 'nullable|integer|min:0',
        ]);

        $category->update(array_merge($validated, ['updated_by' => Auth::id()]));

        return response()->json([
            'success' => true,
            'message' => '글로벌 카테고리가 수정되었습니다.',
            'data' => $category->fresh(),
        ]);
    }

    /**
     * 삭제
     */
    public function destroy(int $id): JsonResponse
    {
        $category = GlobalCategory::findOrFail($id);

        // 하위 카테고리 존재 여부 확인
        if (GlobalCategory::where('parent_id', $id)->whereNull('deleted_at')->exists()) {
            return response()->json([
                'success' => false,
                'message' => '하위 카테고리가 있어 삭제할 수 없습니다.',
            ], 400);
        }

        $category->update(['deleted_by' => Auth::id()]);
        $category->delete();

        return response()->json([
            'success' => true,
            'message' => '글로벌 카테고리가 삭제되었습니다.',
        ]);
    }

    /**
     * 활성 상태 토글
     */
    public function toggle(int $id): JsonResponse
    {
        $category = GlobalCategory::findOrFail($id);
        $category->update([
            'is_active' => ! $category->is_active,
            'updated_by' => Auth::id(),
        ]);

        return response()->json([
            'success' => true,
            'message' => $category->is_active ? '활성화되었습니다.' : '비활성화되었습니다.',
            'is_active' => $category->is_active,
        ]);
    }

    /**
     * 테넌트로 복사
     */
    public function copyToTenant(int $id): JsonResponse
    {
        $tenantId = session('selected_tenant_id');
        if (! $tenantId) {
            return response()->json(['success' => false, 'message' => '테넌트를 선택해주세요.'], 400);
        }

        $globalCategory = GlobalCategory::findOrFail($id);

        // 활성 레코드 확인
        $exists = Category::query()
            ->where('tenant_id', $tenantId)
            ->where('code_group', $globalCategory->code_group)
            ->where('code', $globalCategory->code)
            ->exists();

        if ($exists) {
            return response()->json(['success' => false, 'message' => '이미 존재하는 카테고리입니다.'], 400);
        }

        // 소프트 삭제된 레코드 확인 → 복원
        $trashed = Category::withoutGlobalScopes()->onlyTrashed()
            ->where('tenant_id', $tenantId)
            ->where('code_group', $globalCategory->code_group)
            ->where('code', $globalCategory->code)
            ->first();

        if ($trashed) {
            $trashed->restore();
            $trashed->update([
                'name' => $globalCategory->name,
                'profile_code' => $globalCategory->profile_code,
                'description' => $globalCategory->description,
                'is_active' => $globalCategory->is_active,
                'sort_order' => $globalCategory->sort_order,
                'updated_by' => Auth::id(),
            ]);

            return response()->json([
                'success' => true,
                'message' => '삭제된 카테고리를 복원하였습니다.',
            ]);
        }

        // 신규 복사
        Category::create([
            'tenant_id' => $tenantId,
            'parent_id' => null,
            'code_group' => $globalCategory->code_group,
            'code' => $globalCategory->code,
            'name' => $globalCategory->name,
            'profile_code' => $globalCategory->profile_code,
            'description' => $globalCategory->description,
            'is_active' => $globalCategory->is_active,
            'sort_order' => $globalCategory->sort_order,
            'created_by' => Auth::id(),
        ]);

        return response()->json([
            'success' => true,
            'message' => '테넌트로 복사되었습니다.',
        ]);
    }

    /**
     * 일괄 테넌트로 복사
     */
    public function bulkCopyToTenant(Request $request): JsonResponse
    {
        $tenantId = session('selected_tenant_id');
        if (! $tenantId) {
            return response()->json(['success' => false, 'message' => '테넌트를 선택해주세요.'], 400);
        }

        $validated = $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'integer|exists:global_categories,id',
        ]);

        $globalCategories = GlobalCategory::whereIn('id', $validated['ids'])
            ->whereNull('deleted_at')
            ->orderBy('parent_id')
            ->orderBy('sort_order')
            ->get();

        $copied = 0;
        $skipped = 0;
        $idMap = []; // global_id => tenant_id

        DB::beginTransaction();
        try {
            foreach ($globalCategories as $gc) {
                // 활성 레코드 확인
                $exists = Category::query()
                    ->where('tenant_id', $tenantId)
                    ->where('code_group', $gc->code_group)
                    ->where('code', $gc->code)
                    ->exists();

                if ($exists) {
                    $skipped++;

                    continue;
                }

                // 소프트 삭제된 레코드 확인 → 복원
                $trashed = Category::withoutGlobalScopes()->onlyTrashed()
                    ->where('tenant_id', $tenantId)
                    ->where('code_group', $gc->code_group)
                    ->where('code', $gc->code)
                    ->first();

                if ($trashed) {
                    $trashed->restore();
                    $trashed->update([
                        'name' => $gc->name,
                        'profile_code' => $gc->profile_code,
                        'description' => $gc->description,
                        'is_active' => $gc->is_active,
                        'sort_order' => $gc->sort_order,
                        'updated_by' => Auth::id(),
                    ]);
                    $idMap[$gc->id] = $trashed->id;
                    $copied++;

                    continue;
                }

                // parent_id 매핑
                $parentId = null;
                if ($gc->parent_id && isset($idMap[$gc->parent_id])) {
                    $parentId = $idMap[$gc->parent_id];
                }

                $newCategory = Category::create([
                    'tenant_id' => $tenantId,
                    'parent_id' => $parentId,
                    'code_group' => $gc->code_group,
                    'code' => $gc->code,
                    'name' => $gc->name,
                    'profile_code' => $gc->profile_code,
                    'description' => $gc->description,
                    'is_active' => $gc->is_active,
                    'sort_order' => $gc->sort_order,
                    'created_by' => Auth::id(),
                ]);

                $idMap[$gc->id] = $newCategory->id;
                $copied++;
            }

            DB::commit();

            $message = "{$copied}개 카테고리가 복사되었습니다.";
            if ($skipped > 0) {
                $message .= " ({$skipped}개 중복으로 건너뜀)";
            }

            return response()->json([
                'success' => true,
                'message' => $message,
                'copied' => $copied,
                'skipped' => $skipped,
            ]);
        } catch (\Exception $e) {
            DB::rollBack();

            return response()->json([
                'success' => false,
                'message' => '복사 중 오류가 발생했습니다.',
            ], 500);
        }
    }
}
