<?php

namespace App\Http\Controllers;

use App\Models\Admin\AdminPmProject;
use App\Models\Admin\AdminPmTask;
use App\Services\ProjectManagement\ImportService;
use App\Services\ProjectManagement\ProjectService;
use Illuminate\View\View;

class ProjectManagementController extends Controller
{
    public function __construct(
        private readonly ProjectService $projectService
    ) {}

    /**
     * 프로젝트 관리 대시보드
     */
    public function index(): View
    {
        $summary = $this->projectService->getDashboardSummary();
        $statuses = AdminPmProject::getStatuses();
        $taskStatuses = AdminPmTask::getStatuses();
        $priorities = AdminPmTask::getPriorities();

        return view('project-management.index', compact(
            'summary',
            'statuses',
            'taskStatuses',
            'priorities'
        ));
    }

    /**
     * 프로젝트 목록 화면
     */
    public function projects(): View
    {
        $statuses = AdminPmProject::getStatuses();

        return view('project-management.projects.index', compact('statuses'));
    }

    /**
     * 프로젝트 생성 화면
     */
    public function createProject(): View
    {
        $statuses = AdminPmProject::getStatuses();

        return view('project-management.projects.create', compact('statuses'));
    }

    /**
     * 프로젝트 수정 화면
     */
    public function editProject(int $id): View
    {
        $project = $this->projectService->getProjectById($id, true);

        if (! $project) {
            abort(404, '프로젝트를 찾을 수 없습니다.');
        }

        $statuses = AdminPmProject::getStatuses();

        return view('project-management.projects.edit', compact('project', 'statuses'));
    }

    /**
     * 프로젝트 상세 화면 (작업/이슈 목록 포함)
     */
    public function showProject(int $id): View
    {
        $project = $this->projectService->getProjectById($id, true);

        if (! $project) {
            abort(404, '프로젝트를 찾을 수 없습니다.');
        }

        $statuses = AdminPmProject::getStatuses();
        $taskStatuses = AdminPmTask::getStatuses();
        $priorities = AdminPmTask::getPriorities();

        return view('project-management.projects.show', compact(
            'project',
            'statuses',
            'taskStatuses',
            'priorities'
        ));
    }

    /**
     * JSON Import 화면
     */
    public function import(ImportService $importService): View
    {
        $template = $importService->getSampleTemplate();
        $projects = $this->projectService->getActiveProjects();

        return view('project-management.import', compact('template', 'projects'));
    }
}
