<?php

declare(strict_types=1);

namespace App\Domain\Reseller\Models;

use App\Domain\Package\Models\Package;
use App\Models\User;
use Database\Factories\ResellerSubPackageFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
 * Reseller'ın üst paketinden türettiği alt-paket.
 *
 * @property int $id
 * @property int $reseller_user_id
 * @property int $parent_package_id
 * @property string $name
 * @property string $slug
 * @property array $limits
 * @property float|null $reseller_price_try
 * @property bool $is_active
 * @property int $display_order
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property-read User $reseller
 * @property-read Package $parentPackage
 */
class ResellerSubPackage extends Model
{
    /** @use HasFactory<ResellerSubPackageFactory> */
    use HasFactory;

    protected $table = 'reseller_sub_packages';

    protected $fillable = [
        'reseller_user_id',
        'parent_package_id',
        'name',
        'slug',
        'limits',
        'reseller_price_try',
        'is_active',
        'display_order',
    ];

    protected function casts(): array
    {
        return [
            'limits' => 'array',
            'reseller_price_try' => 'decimal:2',
            'is_active' => 'boolean',
            'display_order' => 'integer',
        ];
    }

    // ─── Relationships ────────────────────────────────────────────────────

    public function reseller(): BelongsTo
    {
        return $this->belongsTo(User::class, 'reseller_user_id');
    }

    public function parentPackage(): BelongsTo
    {
        return $this->belongsTo(Package::class, 'parent_package_id');
    }

    // ─── Helpers ──────────────────────────────────────────────────────────

    public function limit(string $key, mixed $default = null): mixed
    {
        return $this->limits[$key] ?? $default;
    }
}
