<?php

namespace App\Http\Controllers;

use App\Models\Rd\AiQuotation;
use App\Services\Rd\AiQuotationService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class RdController extends Controller
{
    public function __construct(
        private readonly AiQuotationService $quotationService
    ) {}

    /**
     * R&D 대시보드
     */
    public function index(Request $request): View|\Illuminate\Http\Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('rd.index'));
        }

        $dashboard = $this->quotationService->getDashboardStats();
        $statuses = AiQuotation::getStatuses();

        return view('rd.index', compact('dashboard', 'statuses'));
    }

    /**
     * AI 견적 목록
     */
    public function quotations(Request $request): View|\Illuminate\Http\Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('rd.ai-quotation.index'));
        }

        $statuses = AiQuotation::getStatuses();

        return view('rd.ai-quotation.index', compact('statuses'));
    }

    /**
     * AI 견적 생성 폼
     */
    public function createQuotation(Request $request): View|\Illuminate\Http\Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('rd.ai-quotation.create'));
        }

        return view('rd.ai-quotation.create');
    }

    /**
     * AI 견적 문서 (인쇄용 견적서)
     */
    public function documentQuotation(Request $request, int $id): View
    {
        $quotation = $this->quotationService->getById($id);

        if (! $quotation || ! $quotation->isCompleted()) {
            abort(404, '완료된 견적만 문서로 조회할 수 있습니다.');
        }

        $template = $request->query('template', 'classic');
        $allowed = ['classic', 'modern', 'blue', 'dark', 'colorful'];
        if (! in_array($template, $allowed)) {
            $template = 'classic';
        }

        return view('rd.ai-quotation.document', compact('quotation', 'template'));
    }

    /**
     * AI 견적 상세
     */
    public function showQuotation(Request $request, int $id): View|\Illuminate\Http\Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('rd.ai-quotation.show', $id));
        }

        $quotation = $this->quotationService->getById($id);

        if (! $quotation) {
            abort(404, 'AI 견적을 찾을 수 없습니다.');
        }

        return view('rd.ai-quotation.show', compact('quotation'));
    }

    /**
     * AI 견적 편집 (제조 모드)
     */
    public function editQuotation(Request $request, int $id): View|\Illuminate\Http\Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('rd.ai-quotation.edit', $id));
        }

        $quotation = $this->quotationService->getById($id);

        if (! $quotation) {
            abort(404, 'AI 견적을 찾을 수 없습니다.');
        }

        if (! $quotation->isCompleted()) {
            abort(403, '완료된 견적만 편집할 수 있습니다.');
        }

        return view('rd.ai-quotation.edit', compact('quotation'));
    }
}
