<?php

namespace App\Integrations\Semrush\Requests;

use App\Integrations\Common\Configurable;

final class EnableTrackingRequest extends Configurable implements RequestInterface
{
    protected $options = [
        'url' => 'https://api.semrush.com/management/v1/projects/',
        'suffix' => '/tracking/enable',
        'method' => 'POST',
        'trackingUrlType' => 'rootdomain',
        'countryId' => 2826,
        'weeklyNotification' => 0,
    ];

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return string
     */
    public function getMethod(): string
    {
        return $this->getOption('method');
    }

    public function getUri()
    {
        return $this->getOption('url')
            .$this->getOption('projectId')
            .$this->getOption('suffix')
            .'?key='
            .$this->getOption('key');
    }

    /**
     * @param array $body
     *
     * @return $this
     */
    public function setBody(array $body): self
    {
        $this->setOption('body', $body);

        return $this;
    }

    public function getBody()
    {
        return [
            'tracking_url_type' => $this->getOption('trackingUrlType'),
            'tracking_url' => $this->getOption('trackingUrl'),
            'country_id' => $this->getOption('countryId'),
            'weekly_notification' => $this->getOption('weeklyNotification'),
        ];
    }

    public function setProjectId(string $projectId)
    {
        $this->setOption('projectId', $projectId);

        return $this;
    }

    /**
     * @return mixed
     *
     * @throws \Exception
     */
    public function getProjectId()
    {
        if (null === $this->getOption('projectId')) {
            throw new \Exception('Semrush project id is not set');
        }

        return $this->getOption('projectId');
    }

    public function setTrackingUrl(string $trackingUrl)
    {
        $this->setOption('trackingUrl', $trackingUrl);

        return $this;
    }
}
