<?php

declare(strict_types=1);

namespace App\Domain\Dns\Models;

use App\Domain\Account\Models\Account;
use Database\Factories\DnsZoneFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class DnsZone extends Model
{
    /** @use HasFactory<DnsZoneFactory> */
    use HasFactory, SoftDeletes;

    protected $table = 'dns_zones';

    protected $fillable = [
        'account_id', 'name', 'master', 'type', 'notified_serial',
        'last_check', 'powerdns_account', 'status', 'last_error',
        // DNSSEC (Phase 2)
        'dnssec_enabled', 'dnssec_algo', 'ds_records', 'dnssec_keys',
        'dnssec_enabled_at', 'dnssec_last_rollover_at',
    ];

    /** @var array<string,string> */
    protected $casts = [
        'dnssec_enabled' => 'boolean',
        'ds_records' => 'array',
        'dnssec_keys' => 'array',
        'dnssec_enabled_at' => 'datetime',
        'dnssec_last_rollover_at' => 'datetime',
    ];

    public function account(): BelongsTo
    {
        return $this->belongsTo(Account::class);
    }

    public function records(): HasMany
    {
        return $this->hasMany(DnsRecord::class, 'zone_id');
    }

    protected static function newFactory(): DnsZoneFactory
    {
        return DnsZoneFactory::new();
    }
}
