<?php

declare(strict_types=1);

namespace App\Models;

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

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

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

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

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

    public function slotCount(): int
    {
        return (int) ($this->duration_hours * 60 / config('studio.slot_minutes'));
    }

    /**
     * Total for a given party size.
     *
     * Guests beyond the included allowance are charged individually; the max
     * is enforced by the caller rather than silently clamped here, so an
     * over-size party is refused rather than quietly billed for fewer people
     * than are turning up.
     */
    public function priceFor(?int $guests): string
    {
        $base = (string) $this->base_price;

        if ($guests === null || $this->extra_guest_price === null) {
            return $base;
        }

        $extra = max(0, $guests - $this->included_guests);

        if ($extra === 0) {
            return $base;
        }

        return bcadd($base, bcmul((string) $this->extra_guest_price, (string) $extra, 2), 2);
    }

    public function acceptsGuests(?int $guests): bool
    {
        if ($guests === null || $this->max_guests === null) {
            return true;
        }

        return $guests >= 1 && $guests <= $this->max_guests;
    }
}
