<?php

declare(strict_types=1);

/**
 * APO-195 Card 3 — Category action service.
 *
 * Wraps MyronContentRepository::{createPageCategory, updatePageCategory,
 * setPageCategoryActive}. Both admin form path and API path call this.
 *
 * 4 verbs: create, update, archive (deactivate), reactivate.
 */
final class MyronCategoryActionService
{
    public function __construct(
        private readonly MyronContentRepository $repository,
    ) {
    }

    /**
     * @return array{categoryId: string}
     */
    public function create(string $title, string $slug, string $description, MyronActionCallerContext $caller): array
    {
        if (trim($title) === '') {
            throw new InvalidArgumentException('title is required.');
        }
        $id = $this->repository->createPageCategory(trim($title), trim($slug), trim($description));
        return ['categoryId' => (string) $id];
    }

    /**
     * @return array{categoryId: string}
     */
    public function update(
        string $categoryId,
        string $title,
        string $slug,
        string $description,
        bool $isActive,
        MyronActionCallerContext $caller
    ): array {
        if (trim($categoryId) === '') {
            throw new InvalidArgumentException('categoryId is required.');
        }
        $this->repository->updatePageCategory(trim($categoryId), trim($title), trim($slug), trim($description), $isActive);
        return ['categoryId' => trim($categoryId)];
    }

    /**
     * @return array{categoryId: string, isActive: bool}
     */
    public function archive(string $categoryId, MyronActionCallerContext $caller): array
    {
        if (trim($categoryId) === '') {
            throw new InvalidArgumentException('categoryId is required.');
        }
        $this->repository->setPageCategoryActive(trim($categoryId), false);
        return ['categoryId' => trim($categoryId), 'isActive' => false];
    }

    /**
     * @return array{categoryId: string, isActive: bool}
     */
    public function reactivate(string $categoryId, MyronActionCallerContext $caller): array
    {
        if (trim($categoryId) === '') {
            throw new InvalidArgumentException('categoryId is required.');
        }
        $this->repository->setPageCategoryActive(trim($categoryId), true);
        return ['categoryId' => trim($categoryId), 'isActive' => true];
    }
}
