<?php

namespace App\Sales;

use App\Facades\Settings;
use App\Facades\Site;
use App\Traits\FormatAsCurrency;
use Illuminate\Support\Facades\Log;
use Mtc\MercuryDataModels\Vehicle;

class SilverBullet
{
    use FormatAsCurrency;

    public function getVehicleAction(Vehicle $vehicle, string $dealType = '')
    {
        if (!empty($vehicle->dealership->data['silver-bullet-disabled'])) {
            return [];
        }

        $deposit = $this->getDepositValue($vehicle);
        return [
            'type' => 'component',
            'value' => 'CatalogSilverBulletDeal',
            'data' => [
                'action' => $dealType,
                'title' => $this->getTitle($vehicle, $dealType),
                'url' => $this->url(Settings::get('sales-silver-bullet-domain'), $dealType),
                'id' => $vehicle->id,
                'vehicle_id' => $vehicle->uuid ?? $vehicle->id,
                'dealer_id' => $vehicle->dealership->data['silver-bullet-dealer-id'] ?? $vehicle->dealership_id,
                'utm_source' => Settings::get('sales-silver-bullet-campaign-source'),
                'utm_medium' => Settings::get('sales-silver-bullet-campaign-medium'),
                'utm_campaign' => Settings::get('sales-silver-bullet-campaign-name'),
                'term' => Settings::get('sales-silver-bullet-term'),
                'deposit' => $deposit,
                'mileage' => Settings::get('sales-silver-bullet-annual-mileage'),
                'vehicle_url' => Site::vehicleUrl($vehicle),
            ]
        ];
    }

    /**
     * Deal building url. Determined by type
     *
     * @param string|null $domain
     * @param string $dealType
     * @return string
     */
    private function url(?string $domain, string $dealType): string
    {
        return match ($dealType) {
            'finance' => rtrim((string) $domain, '/') . '/start-finance/',
            'valuation' => rtrim((string) $domain, '/') . '/start-trade-in/',
            'reserve' => rtrim((string) $domain, '/') . '/start/?open_reserve_modal',
            default => rtrim((string) $domain, '/') . '/start/',
        };
    }

    private function getTitleSetting(string $dealType)
    {
        return match ($dealType) {
            'finance' => 'sales-silver-bullet-finance-button-text',
            'valuation' => 'sales-silver-bullet-trade-in-button-text',
            'reserve' => 'sales-silver-bullet-reserve-button-text',
            default => 'sales-silver-bullet-deal-button-text',
        };
    }

    private function getTitle(Vehicle $vehicle, string $dealType)
    {
        $title = Settings::get($this->getTitleSetting($dealType));
        if ($dealType === 'reserve') {
            $cost = $vehicle->dealership->data['silver-bullet-reserve-cost'] ??
                Settings::get('sales-silver-bullet-reserve-cost');
            $cost = $cost ? $this->asCurrency($cost) : '';
            $title = str_replace(':amount', $cost, $title);
        }

        return $title;
    }

    private function getDepositValue(Vehicle $vehicle)
    {
        $below_treshold = Settings::get('finance-deposit-flat-rate-treshold') > 0
            && $vehicle->price < Settings::get('finance-deposit-flat-rate-treshold');

        return $below_treshold
            ? Settings::get('finance-deposit-flat-rate-amount')
            : round(Settings::get('sales-silver-bullet-deposit') * 0.01 * $vehicle->price);
    }
}
