<?php

namespace Edoobox\Requests;

use Edoobox\Base\Options;
use Edoobox\Base\Settings;
use Edoobox\Helpers\Arr;

class NextOffer {

    public array $offer = [];
    public array $options = [];
    public array $response = [];
    public string $pluginUrl = "";

    public function __construct(array $offer, array $response) {
        $this->offer = $offer;
        $this->options = Options::get(Settings::SETTINGS_OPTION_NAME, []);
        $this->response = $response;
        $this->pluginUrl = plugin_dir_url(dirname(__FILE__, 2));
    }

    public function getDateFormatted($key): string {
        $date = Arr::get($this->offer, $key);

        if ( ! $date ) {
            return '';
        }

        try {
            $dateTime = new \DateTime($date);
            $dateFormat = Options::get('date_format'); // Fetch date format from WordPress settings
            $timeFormat = Options::get('time_format'); // Fetch time format from WordPress settings
            return $dateTime->format($dateFormat . ' ' . $timeFormat);
        } catch ( \Exception $e ) {
            return '';
        }
    }

    public function getImageUrl(): string {
        return Arr::get($this->offer, 'image') ?: "$this->pluginUrl/assets/og-fallback-offer-image-compressed.png";
    }

    public function getRewriteUrl(): string {
        return Arr::get($this->response, 'provider_url') . $this->getOption('account_code') . '/' . Arr::get($this->offer, 'offer_details_url');
    }

    public function toArray(): array {
        return [

        ];
    }

    private function getOption(string $key) {
        return Arr::get($this->options, $key);
    }
}