<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PlanProvider extends Model
{
    use HasFactory;

    protected $fillable = [
        'plan_id',
        'provider',
        'provider_price_id',
        'provider_variant_id',
        'provider_product_id',
        'is_enabled',
        'price',
        'currency',
        'provider_data',
        'sort_order',
    ];

    protected $casts = [
        'is_enabled' => 'boolean',
        'price' => 'decimal:2',
        'provider_data' => 'array',
    ];

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

    public function scopeEnabled($query)
    {
        return $query->where('is_enabled', true);
    }

    public function scopeByProvider($query, string $provider)
    {
        return $query->where('provider', $provider);
    }

    public function getProviderData(?string $key = null, $default = null)
    {
        if ($key) {
            return data_get($this->provider_data, $key, $default);
        }

        return $this->provider_data;
    }
}
