<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Api\Resource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Administration\Domain\Model\SchoolBranding\SchoolBranding;
use App\Administration\Infrastructure\Api\Processor\DeleteLogoProcessor;
use App\Administration\Infrastructure\Api\Processor\UpdateBrandingColorsProcessor;
use App\Administration\Infrastructure\Api\Processor\UploadLogoProcessor;
use App\Administration\Infrastructure\Api\Provider\SchoolBrandingProvider;
use DateTimeImmutable;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * API Resource pour la gestion du branding de l'établissement.
 *
 * @see FR83 - Configurer logo et couleurs établissement
 */
#[ApiResource(
    shortName: 'SchoolBranding',
    operations: [
        new Get(
            uriTemplate: '/school/branding',
            provider: SchoolBrandingProvider::class,
            name: 'get_school_branding',
        ),
        new Put(
            uriTemplate: '/school/branding',
            read: false,
            processor: UpdateBrandingColorsProcessor::class,
            validationContext: ['groups' => ['Default', 'update']],
            name: 'update_school_branding',
        ),
        new Post(
            uriTemplate: '/school/branding/logo',
            read: false,
            deserialize: false,
            processor: UploadLogoProcessor::class,
            name: 'upload_school_logo',
        ),
        new Delete(
            uriTemplate: '/school/branding/logo',
            read: false,
            processor: DeleteLogoProcessor::class,
            name: 'delete_school_logo',
        ),
    ],
)]
final class SchoolBrandingResource
{
    #[ApiProperty(identifier: true)]
    public ?string $schoolId = null;

    public ?string $logoUrl = null;

    public ?DateTimeImmutable $logoUpdatedAt = null;

    #[Assert\Regex(
        pattern: '/^#[0-9A-Fa-f]{6}$/',
        message: 'La couleur doit être au format hexadécimal #RRGGBB.',
        groups: ['update'],
    )]
    public ?string $primaryColor = null;

    #[Assert\Regex(
        pattern: '/^#[0-9A-Fa-f]{6}$/',
        message: 'La couleur doit être au format hexadécimal #RRGGBB.',
        groups: ['update'],
    )]
    public ?string $secondaryColor = null;

    #[Assert\Regex(
        pattern: '/^#[0-9A-Fa-f]{6}$/',
        message: 'La couleur doit être au format hexadécimal #RRGGBB.',
        groups: ['update'],
    )]
    public ?string $accentColor = null;

    public ?DateTimeImmutable $updatedAt = null;

    public static function fromDomain(SchoolBranding $branding): self
    {
        $resource = new self();
        $resource->schoolId = (string) $branding->schoolId;
        $resource->logoUrl = $branding->logoUrl !== null ? (string) $branding->logoUrl : null;
        $resource->logoUpdatedAt = $branding->logoUpdatedAt;
        $resource->primaryColor = $branding->primaryColor !== null ? (string) $branding->primaryColor : null;
        $resource->secondaryColor = $branding->secondaryColor !== null ? (string) $branding->secondaryColor : null;
        $resource->accentColor = $branding->accentColor !== null ? (string) $branding->accentColor : null;
        $resource->updatedAt = $branding->updatedAt;

        return $resource;
    }
}
