<?php

namespace App\Http\Controllers\Api\Admin\ProjectManagement;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProjectManagement\ImportIssuesRequest;
use App\Http\Requests\ProjectManagement\ImportProjectRequest;
use App\Services\ProjectManagement\ImportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ImportController extends Controller
{
    public function __construct(
        private readonly ImportService $importService
    ) {}

    /**
     * JSON으로 프로젝트 일괄 등록
     */
    public function import(ImportProjectRequest $request): JsonResponse
    {
        try {
            $result = $this->importService->importFromJson($request->validated());

            return response()->json([
                'success' => true,
                'message' => "프로젝트가 생성되었습니다. (작업: {$result['tasks_count']}개, 이슈: {$result['issues_count']}개)",
                'data' => [
                    'project_id' => $result['project']->id,
                    'project_name' => $result['project']->name,
                    'tasks_count' => $result['tasks_count'],
                    'issues_count' => $result['issues_count'],
                ],
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '가져오기 실패: '.$e->getMessage(),
            ], 500);
        }
    }

    /**
     * 기존 프로젝트에 작업/이슈 추가
     */
    public function importTasks(Request $request, int $projectId): JsonResponse
    {
        $validated = $request->validate([
            'tasks' => 'required|array|min:1',
            'tasks.*.title' => 'required|string|max:255',
            'tasks.*.description' => 'nullable|string',
            'tasks.*.status' => 'nullable|in:todo,in_progress,done',
            'tasks.*.priority' => 'nullable|in:low,medium,high',
            'tasks.*.due_date' => 'nullable|date',
            'tasks.*.issues' => 'nullable|array',
            'tasks.*.issues.*.title' => 'required|string|max:255',
            'tasks.*.issues.*.description' => 'nullable|string',
            'tasks.*.issues.*.type' => 'nullable|in:bug,feature,improvement',
            'tasks.*.issues.*.status' => 'nullable|in:open,in_progress,resolved,closed',
        ]);

        try {
            $result = $this->importService->importTasksToProject($projectId, $validated['tasks']);

            return response()->json([
                'success' => true,
                'message' => "작업이 추가되었습니다. (작업: {$result['tasks_count']}개, 이슈: {$result['issues_count']}개)",
                'data' => $result,
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '가져오기 실패: '.$e->getMessage(),
            ], 500);
        }
    }

    /**
     * 기존 작업에 이슈만 추가
     */
    public function importIssues(ImportIssuesRequest $request, int $taskId): JsonResponse
    {
        try {
            $result = $this->importService->importIssuesToTask($taskId, $request->validated()['issues']);

            return response()->json([
                'success' => true,
                'message' => "이슈가 추가되었습니다. ({$result['issues_count']}개)",
                'data' => $result,
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => '가져오기 실패: '.$e->getMessage(),
            ], 500);
        }
    }

    /**
     * JSON 구조 사전 검증
     */
    public function validate(Request $request): JsonResponse
    {
        $data = $request->all();
        $errors = $this->importService->validateJsonStructure($data);

        if (! empty($errors)) {
            return response()->json([
                'success' => false,
                'message' => 'JSON 구조가 올바르지 않습니다.',
                'errors' => $errors,
            ], 422);
        }

        return response()->json([
            'success' => true,
            'message' => 'JSON 구조가 유효합니다.',
        ]);
    }

    /**
     * 샘플 JSON 템플릿 반환
     */
    public function template(): JsonResponse
    {
        return response()->json([
            'success' => true,
            'data' => $this->importService->getSampleTemplate(),
        ]);
    }
}
