<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property int $id
 * @property int $schedule_id
 * @property string $origin
 * @property int $origin_id
 * @property numeric $stars
 * @property string|null $comment
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ImprovementType> $improvements
 * @property-read int|null $improvements_count
 * @property-read \App\Models\Client|null $originClient
 * @property-read \App\Models\Provider|null $originProvider
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ReviewMedia> $reviewMedia
 * @property-read int|null $review_media_count
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ReviewImprovement> $reviewsImprovements
 * @property-read int|null $reviews_improvements_count
 * @property-read \App\Models\Schedule $schedule
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereComment($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereOrigin($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereOriginId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereScheduleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereStars($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review withTrashed(bool $withTrashed = true)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Review withoutTrashed()
 * @mixin \Eloquent
 */
class Review extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'schedule_id',
        'origin',
        'origin_id',
        'stars',
        'comment',
    ];

    protected $casts = [
        'stars'      => 'decimal:1',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

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

    //

    public function originClient(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'origin_id');
    }

    public function originProvider(): BelongsTo
    {
        return $this->belongsTo(Provider::class, 'origin_id');
    }

    //

    public function improvements(): BelongsToMany
    {
        return $this->belongsToMany(
            ImprovementType::class,
            'reviews_improvements',
            'review_id',
            'improvement_type_id'
        )->withTimestamps();
    }

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

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