<?php

namespace App\Campaigns;

use Illuminate\Support\Carbon;

class PerformanceProfileView
{
    public function __construct(
        public ?string $name = null,
        public ?string $objective = null,
        public ?string $delivery_status = null,
        public ?string $platform_status = null,
        public ?string $account_id = null,
        public ?Carbon $started_at = null,
        public ?string $region_label = null,
        public ?string $platform_learning_label = null,
        public ?float $daily_budget = null,
    ) {}

    /**
     * @param  array<string, mixed>  $profile
     */
    public static function fromSnapshot(array $profile): self
    {
        $started = $profile['started_at'] ?? null;

        return new self(
            name: isset($profile['name']) ? (string) $profile['name'] : null,
            objective: isset($profile['objective']) ? (string) $profile['objective'] : null,
            delivery_status: isset($profile['delivery_status']) ? (string) $profile['delivery_status'] : null,
            platform_status: isset($profile['platform_status']) ? (string) $profile['platform_status'] : null,
            account_id: isset($profile['account_id']) ? (string) $profile['account_id'] : null,
            started_at: is_string($started) && $started !== '' ? Carbon::parse($started) : null,
            region_label: isset($profile['region_label']) ? (string) $profile['region_label'] : null,
            platform_learning_label: isset($profile['platform_learning_label'])
                ? (string) $profile['platform_learning_label']
                : null,
            daily_budget: isset($profile['daily_budget']) ? (float) $profile['daily_budget'] : null,
        );
    }

    public function regionLabel(): ?string
    {
        return filled($this->region_label) ? $this->region_label : null;
    }

    public function platformLearningLabel(): ?string
    {
        return filled($this->platform_learning_label) ? $this->platform_learning_label : null;
    }
}
