<?php

namespace App\Services;

use App\Models\HeroCard;
use Illuminate\Database\Eloquent\Collection;

class HeroCardService
{
    /**
     * Return all active hero cards ordered by `order` ascending.
     * Used by the homepage to populate the info cards section.
     */
    public function getActive(): Collection
    {
        return HeroCard::active()->get();
    }

    /**
     * Return all hero cards ordered by `order` ascending.
     * Used by the admin index listing.
     */
    public function getAll(): Collection
    {
        return HeroCard::orderBy('order', 'asc')->get();
    }

    /**
     * Create a new HeroCard record.
     * Strips HTML tags from `title` and `content` before persisting (Requirement 6.6).
     */
    public function create(array $data): HeroCard
    {
        $data['title']   = strip_tags($data['title'] ?? '');
        $data['content'] = strip_tags($data['content'] ?? '');

        return HeroCard::create($data);
    }

    /**
     * Update an existing HeroCard record.
     * Strips HTML tags from `title` and `content` before persisting (Requirement 6.6).
     */
    public function update(HeroCard $heroCard, array $data): HeroCard
    {
        if (isset($data['title'])) {
            $data['title'] = strip_tags($data['title']);
        }

        if (isset($data['content'])) {
            $data['content'] = strip_tags($data['content']);
        }

        $heroCard->update($data);

        return $heroCard->fresh();
    }

    /**
     * Delete a HeroCard record permanently.
     */
    public function delete(HeroCard $heroCard): bool
    {
        return (bool) $heroCard->delete();
    }
}
