<?php

declare(strict_types=1);

namespace App\Scolarite\Domain\Model\Grade;

use App\Administration\Domain\Model\User\UserId;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;

final class AppreciationTemplate
{
    public private(set) DateTimeImmutable $updatedAt;
    public private(set) int $usageCount;

    private function __construct(
        public private(set) AppreciationTemplateId $id,
        public private(set) TenantId $tenantId,
        public private(set) UserId $teacherId,
        public private(set) string $title,
        public private(set) string $content,
        public private(set) ?AppreciationCategory $category,
        public private(set) DateTimeImmutable $createdAt,
    ) {
        $this->updatedAt = $createdAt;
        $this->usageCount = 0;
    }

    public static function creer(
        TenantId $tenantId,
        UserId $teacherId,
        string $title,
        string $content,
        ?AppreciationCategory $category,
        DateTimeImmutable $now,
    ): self {
        return new self(
            id: AppreciationTemplateId::generate(),
            tenantId: $tenantId,
            teacherId: $teacherId,
            title: $title,
            content: $content,
            category: $category,
            createdAt: $now,
        );
    }

    public function modifier(
        string $title,
        string $content,
        ?AppreciationCategory $category,
        DateTimeImmutable $now,
    ): void {
        $this->title = $title;
        $this->content = $content;
        $this->category = $category;
        $this->updatedAt = $now;
    }

    public function incrementerUtilisation(): void
    {
        ++$this->usageCount;
    }

    /**
     * @internal Pour usage Infrastructure uniquement
     */
    public static function reconstitute(
        AppreciationTemplateId $id,
        TenantId $tenantId,
        UserId $teacherId,
        string $title,
        string $content,
        ?AppreciationCategory $category,
        int $usageCount,
        DateTimeImmutable $createdAt,
        DateTimeImmutable $updatedAt,
    ): self {
        $template = new self(
            id: $id,
            tenantId: $tenantId,
            teacherId: $teacherId,
            title: $title,
            content: $content,
            category: $category,
            createdAt: $createdAt,
        );

        $template->updatedAt = $updatedAt;
        $template->usageCount = $usageCount;

        return $template;
    }
}
