<?php

namespace SmartSyncS3\Models;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class SMARL_CustomUrl
{

    public string $certificateArn = "";

    public string $domain = "";

    public array $records = [];

    public static string $optionName = "custom_url_option";

    public function __construct(
        string $certificateArn = "",
        string $domain = "",
        array $records = [],

    ) {
        $this->certificateArn = $certificateArn;
        $this->domain = $domain;
        $this->records = $records;
    }

    public static function load(): SMARL_CustomUrl {
        $data = get_option(self::$optionName, []);

        return self::fromArray($data);
    }

    public static function fromArray(array $data): SMARL_CustomUrl {

        return new SMARL_CustomUrl(
            certificateArn: $data['certificate_arn'] ?? '',
            domain: $data["domain"] ?? "",
            records: $data["records"] ?? []
        );
    }

    public function reset(): void {
        $defaultInstance = new self();
        foreach (get_object_vars($defaultInstance) as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }
        $this->save();
    }


    public function save(): void {
        update_option(self::$optionName, $this->toArray());
    }

    public function toArray(): array {


        return [
            'certificate_arn' => $this->certificateArn,
            'domain' => $this->domain,
            'records' => $this->records,
        ];
    }

}
