<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Jobs\SendRequestTo1CJob;

/**
 * @method static create(array $array)
 * @method static whereBetween(string $string, array $array)
 * @method static selectRaw(string $string)
 * @method static where(string $string, mixed $id)
 */
class InstallRequest extends Model
{
    protected $fillable = [
        'title',
        'description',
        'address',
        'scheduled_date',
        'status',
        'client_name',
        'client_phone',
        'created_by',
        'assigned_to',
        'region',
        'subject',
        'completed_at'
    ];

    protected array $dates = ['scheduled_date'];

    public function actions(): HasMany
    {
        return $this->hasMany(InstallRequestAction::class, 'request_id');
    }

    public function ratings(): HasMany
    {
        return $this->hasMany(RequestRating::class, 'request_id');
    }

    public function installer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_to');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

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

    public function consumables(): BelongsToMany
    {
        return $this->belongsToMany(Consumable::class, 'install_request_consumable')
            ->withPivot('quantity')
            ->withTimestamps();
    }
}
