<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreHeroCardRequest;
use App\Http\Requests\UpdateHeroCardRequest;
use App\Models\HeroCard;
use App\Services\HeroCardService;

class HeroCardController extends Controller
{
    private HeroCardService $heroCardService;

    public function __construct(HeroCardService $heroCardService)
    {
        $this->heroCardService = $heroCardService;
    }

    /**
     * List all hero cards — title, background colour, order, active status.
     * Requirement 8.3
     */
    public function index()
    {
        $heroCards = $this->heroCardService->getAll();

        return view('admin.hero-cards.index', compact('heroCards'));
    }

    /**
     * Show the create form.
     * Requirement 8.4
     */
    public function create()
    {
        return view('admin.hero-cards.create');
    }

    /**
     * Persist a new hero card via HeroCardService.
     * Requirement 8.5, 8.6
     */
    public function store(StoreHeroCardRequest $request)
    {
        $data = $request->validated();

        // Ensure is_active defaults to false when checkbox is unchecked (not submitted)
        $data['is_active'] = $request->boolean('is_active');

        $this->heroCardService->create($data);

        return redirect()
            ->route('admin.hero-cards.index')
            ->with('success', 'Hero card created successfully.');
    }

    /**
     * Show the edit form pre-populated with the hero card's data.
     * Requirement 8.7
     */
    public function edit(HeroCard $heroCard)
    {
        return view('admin.hero-cards.edit', compact('heroCard'));
    }

    /**
     * Update the hero card via HeroCardService.
     * Requirement 8.8
     */
    public function update(UpdateHeroCardRequest $request, HeroCard $heroCard)
    {
        $data = $request->validated();

        // Ensure is_active defaults to false when checkbox is unchecked (not submitted)
        $data['is_active'] = $request->boolean('is_active');

        $this->heroCardService->update($heroCard, $data);

        return redirect()
            ->route('admin.hero-cards.index')
            ->with('success', 'Hero card updated successfully.');
    }

    /**
     * Delete the hero card via HeroCardService.
     * Requirement 8.9
     */
    public function destroy(HeroCard $heroCard)
    {
        $this->heroCardService->delete($heroCard);

        return redirect()
            ->route('admin.hero-cards.index')
            ->with('success', 'Hero card deleted successfully.');
    }
}
