<?php

namespace App\Models;

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

class ServicePricing extends Model
{
    use HasFactory;

    protected $table = 'service_pricing';

    protected $fillable = [
        'service_name',
        'pricing_type',
        'price_value',
        'description',
        'is_active',
    ];

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

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

    /**
     * Get pricing for a specific service
     */
    public static function getPricing(string $serviceName): ?self
    {
        return static::where('service_name', $serviceName)
                    ->where('is_active', true)
                    ->first();
    }

    /**
     * Calculate price based on base amount
     */
    public function calculatePrice(float $baseAmount = 0): float
    {
        if ($this->pricing_type === 'percentage') {
            return ($baseAmount * $this->price_value) / 100;
        }
        
        return $this->price_value;
    }
}
