<?php

namespace App\Http\Controllers\Sales;

use App\Http\Controllers\Controller;
use App\Services\Sales\InterviewScenarioService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class InterviewScenarioController extends Controller
{
    public function __construct(
        private InterviewScenarioService $service,
    ) {}

    // ============================================================
    // 페이지
    // ============================================================

    public function index(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('sales.interviews.index'));
        }

        return view('sales.interviews.index');
    }

    // ============================================================
    // 카테고리 API
    // ============================================================

    public function categories(): JsonResponse
    {
        return response()->json($this->service->getCategories());
    }

    public function storeCategory(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'description' => 'nullable|string',
        ]);

        $category = $this->service->createCategory($validated);

        return response()->json($category, 201);
    }

    public function updateCategory(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'description' => 'nullable|string',
        ]);

        $category = $this->service->updateCategory($id, $validated);

        return response()->json($category);
    }

    public function destroyCategory(int $id): JsonResponse
    {
        $this->service->deleteCategory($id);

        return response()->json(['message' => '삭제되었습니다.']);
    }

    // ============================================================
    // 트리 API
    // ============================================================

    public function tree(): JsonResponse
    {
        return response()->json($this->service->getTree());
    }

    // ============================================================
    // 템플릿(항목) API
    // ============================================================

    public function storeTemplate(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'interview_category_id' => 'required|integer|exists:interview_categories,id',
            'name' => 'required|string|max:200',
            'description' => 'nullable|string',
        ]);

        $template = $this->service->createTemplate($validated);

        return response()->json($template, 201);
    }

    public function updateTemplate(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:200',
            'description' => 'nullable|string',
        ]);

        $template = $this->service->updateTemplate($id, $validated);

        return response()->json($template);
    }

    public function destroyTemplate(int $id): JsonResponse
    {
        $this->service->deleteTemplate($id);

        return response()->json(['message' => '삭제되었습니다.']);
    }

    // ============================================================
    // 질문 API
    // ============================================================

    public function storeQuestion(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'interview_template_id' => 'required|integer|exists:interview_templates,id',
            'question_text' => 'required|string|max:500',
            'question_type' => 'nullable|string|in:checkbox,text',
            'is_required' => 'nullable|boolean',
        ]);

        $question = $this->service->createQuestion($validated);

        return response()->json($question, 201);
    }

    public function updateQuestion(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'question_text' => 'required|string|max:500',
            'question_type' => 'nullable|string|in:checkbox,text',
            'is_required' => 'nullable|boolean',
        ]);

        $question = $this->service->updateQuestion($id, $validated);

        return response()->json($question);
    }

    public function destroyQuestion(int $id): JsonResponse
    {
        $this->service->deleteQuestion($id);

        return response()->json(['message' => '삭제되었습니다.']);
    }

    // ============================================================
    // MD 파일 일괄 가져오기
    // ============================================================

    public function bulkImport(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'category_id' => 'required|integer|exists:interview_categories,id',
            'templates' => 'required|array|min:1',
            'templates.*.name' => 'required|string|max:200',
            'templates.*.questions' => 'required|array|min:1',
            'templates.*.questions.*' => 'required|string|max:500',
        ]);

        $result = $this->service->bulkImport($validated['category_id'], $validated['templates']);

        return response()->json($result, 201);
    }

    // ============================================================
    // 세션 API
    // ============================================================

    public function sessions(Request $request): JsonResponse
    {
        $filters = $request->only(['status', 'category_id']);
        $sessions = $this->service->getSessions($filters);

        return response()->json($sessions);
    }

    public function storeSession(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'interview_category_id' => 'required|integer|exists:interview_categories,id',
            'interviewee_name' => 'nullable|string|max:100',
            'interviewee_company' => 'nullable|string|max:200',
            'interview_date' => 'nullable|date',
            'memo' => 'nullable|string',
        ]);

        $session = $this->service->startSession($validated);

        return response()->json($session, 201);
    }

    public function showSession(int $id): JsonResponse
    {
        $session = $this->service->getSessionDetail($id);

        return response()->json($session);
    }

    public function toggleAnswer(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'session_id' => 'required|integer',
            'question_id' => 'required|integer',
            'answer_text' => 'nullable|string',
            'memo' => 'nullable|string',
        ]);

        $answer = $this->service->toggleAnswer($validated);

        return response()->json($answer);
    }

    public function completeSession(int $id): JsonResponse
    {
        $session = $this->service->completeSession($id);

        return response()->json($session);
    }
}
