<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property int $id
 * @property int $tld_id
 * @property \App\Models\Registrar|null $registrar
 * @property bool $is_active
 * @property array<array-key, mixed>|null $metadata
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Pricing> $pricings
 * @property-read int|null $pricings_count
 * @property-read \App\Models\Tld|null $tld
 *
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost active()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost byRegistrar($registrar)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost byTld($tldId)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereIsActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereMetadata($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereRegistrar($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereTldId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost withTrashed(bool $withTrashed = true)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|DomainCost withoutTrashed()
 *
 * @mixin \Eloquent
 */
class DomainCost extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'tld_id',
        'registrar',
        'is_active',
        'metadata',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'metadata' => 'array',
    ];

    /**
     * Get TLD relationship
     */
    public function tld()
    {
        return $this->belongsTo(Tld::class, 'tld_id');
    }

    /**
     * Get registrar relationship (if exists)
     */
    public function registrar()
    {
        return $this->belongsTo(Registrar::class, 'registrar', 'name');
    }

    /**
     * Get pricing records for this domain cost
     */
    public function pricings()
    {
        return $this->hasMany(Pricing::class, 'relation_id')
            ->where('type', 'domain_cost')
            ->orderBy('period', 'asc');
    }

    /**
     * Check if cost is currently active
     */
    public function isCurrentlyActive(): bool
    {
        return (bool) $this->is_active;
    }

    /**
     * Scope for active costs
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope for costs by TLD
     */
    public function scopeByTld($query, $tldId)
    {
        return $query->where('tld_id', $tldId);
    }

    /**
     * Scope for costs by registrar
     */
    public function scopeByRegistrar($query, $registrar)
    {
        return $query->where('registrar', $registrar);
    }
}
