<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CustomSchedule extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'schedule_id',
        'address_type',
        'service_type_id',
        'description',
        'min_price',
        'max_price',
        'offers_meal',
    ];

    protected $casts = [
        'min_price' => 'decimal:2',
        'max_price' => 'decimal:2',
        'offers_meal' => 'boolean',
    ];

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

    public function serviceType(): HasOne
    {
        return $this->hasOne(ServiceType::class, 'id', 'service_type_id')->select('id', 'description');
    }

    public function specialities(): HasMany
    {
        return $this->hasMany(CustomScheduleSpeciality::class, 'custom_schedule_id', 'id')->leftJoin('specialities', 'custom_schedules_specialities.speciality_id', '=', 'specialities.id')
            ->select('specialities.id', 'specialities.description', 'custom_schedules_specialities.custom_schedule_id');
    }
}
