<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Entity\OgLive;
use App\Model\OgLiveStatus;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

final class OgLiveInput
{
    const string DOWNLOAD_URL = 'https://ognproject.evlt.uma.es/oglive//';

    #[Groups(['og-live:write'])]
    #[ApiProperty(description: 'The download url of the ogLive', example: "http://example.com/oglive1.iso")]
    public ?string $downloadUrl = null;

    public function __construct(?OgLive $ogLive = null)
    {
        if (!$ogLive) {
            return;
        }

        $this->downloadUrl = $ogLive->getDownloadUrl();
    }

    public function createOrUpdateEntity(?OgLive $ogLive = null): OgLive
    {
        if (!$ogLive) {
            $ogLive = new OgLive();
        }

        $filename = str_replace(self::DOWNLOAD_URL, '', $this->downloadUrl);

        if (str_ends_with($filename, '.iso')) {
            $filename = substr($filename, 0, -4);
        }

        $ogLive->setName($filename);
        $ogLive->setFilename($filename);
        $ogLive->setDownloadUrl($this->downloadUrl);
        $ogLive->setStatus(OgLiveStatus::INACTIVE);

        return $ogLive;
    }
}