<?php

namespace App\Models;

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

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 originProvider(): BelongsTo
  {
    return $this->belongsTo(Provider::class, 'origin_id');
  }

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

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

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