<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Service extends Model
{
    protected $guarded = [];

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

    public function orders(): HasMany
    {
        return $this->hasMany(ServiceOrder::class);
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }

    public function priceFor(bool $express): string
    {
        $price = (string) $this->price;

        if ($express && $this->express_surcharge !== null) {
            return bcadd($price, (string) $this->express_surcharge, 2);
        }

        return $price;
    }

    public function turnaroundLabel(bool $express): string
    {
        return $express
            ? "within {$this->express_turnaround_hours} hours"
            : "1–{$this->turnaround_days} days";
    }
}
