<?php

namespace App\Http\Controllers\ClaudeCode;

use App\Http\Controllers\Controller;
use App\Services\ClaudeCodeNewsService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class NewsController extends Controller
{
    public function __construct(
        private ClaudeCodeNewsService $newsService
    ) {}

    /**
     * Claude Code 뉴스 (GitHub Releases) 목록
     */
    public function index(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('claude-code.news.index'));
        }

        $releases = $this->newsService->getReleases();

        return view('claude-code.news.index', compact('releases'));
    }

    /**
     * 캐시 새로고침
     */
    public function refreshCache(): RedirectResponse
    {
        $this->newsService->clearCache();

        return redirect()->route('claude-code.news.index')
            ->with('success', '캐시가 새로고침되었습니다.');
    }
}
