<?php

namespace App\Services;

use App\Models\Setting;

class ClinicSettings
{
    public function get(int $organizationId, string $key, mixed $default = null): mixed
    {
        $setting = Setting::query()
            ->where('organization_id', $organizationId)
            ->where('key', $key)
            ->first();

        return $setting?->value ?? $default;
    }

    public function array(int $organizationId, string $key, array $default = []): array
    {
        $value = $this->get($organizationId, $key, $default);

        return is_array($value) ? array_values($value) : $default;
    }

    public function appointmentTypes(int $organizationId): array
    {
        return $this->array($organizationId, 'appointment_types', [
            'new_consultation', 'follow_up', 'routine', 'emergency',
        ]);
    }

    public function paymentMethods(int $organizationId): array
    {
        return $this->array($organizationId, 'payment_methods', [
            'cash', 'card', 'bank_transfer', 'online', 'insurance',
        ]);
    }

    public function reminderOffsets(int $organizationId): array
    {
        return array_values(array_unique(array_map(
            'intval',
            $this->array($organizationId, 'reminder_offsets_minutes', [1440, 120])
        )));
    }

    public function bool(int $organizationId, string $key, bool $default = false): bool
    {
        return (bool) $this->get($organizationId, $key, $default);
    }
}
