<?php

namespace App\Services;

class DomainMonitorService
{
    /**
     * Create a new class instance.
     */
    public function __construct()
    {
        //
    }

     public function attachMonitor(LicenseDomain $domain): Monitor
    {
        if ($domain->monitor_id) {
            return $domain->monitor;
        }

        $monitor = Monitor::create([
            'url' => $this->formatUrl($domain->domain),
        ]);

        $domain->update([
            'monitor_id' => $monitor->id
        ]);

        return $monitor;
    }

    private function formatUrl(string $domain): string
    {
        return str_starts_with($domain, 'http')
            ? $domain
            : 'https://' . $domain;
    }
}
