<?php

namespace App\DTOs;

readonly class CreativeBrief
{
    public function __construct(
        public string $productName,
        public array $benefits,
        public array $features,
        public array $images,
        public string $messaging,
        public string $cta,
        public ?string $projectName = null,
        public ?string $visualDescription = null,
        public ?string $handling = null,
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            productName: $data['product_name'] ?? '',
            benefits: $data['benefits'] ?? [],
            features: $data['features'] ?? [],
            images: $data['images'] ?? [],
            messaging: $data['messaging'] ?? '',
            cta: $data['cta'] ?? '',
            projectName: $data['project_name'] ?? null,
            visualDescription: $data['visual_description'] ?? null,
            handling: self::normalizeHandling($data['handling'] ?? null),
        );
    }

    public function toArray(): array
    {
        return [
            'product_name' => $this->productName,
            'benefits' => $this->benefits,
            'features' => $this->features,
            'images' => $this->images,
            'messaging' => $this->messaging,
            'cta' => $this->cta,
            'project_name' => $this->projectName,
            'visual_description' => $this->visualDescription,
            'handling' => $this->handling,
        ];
    }

    public static function normalizeHandling(mixed $value): ?string
    {
        $handling = strtolower(trim((string) $value));

        return in_array($handling, ['handheld', 'display'], true) ? $handling : null;
    }

    public static function handlingLabel(mixed $value): ?string
    {
        return match (self::normalizeHandling($value)) {
            'handheld' => 'Handheld — presenter can hold it',
            'display' => 'Display — product stays in place',
            default => null,
        };
    }
}
