<?php

namespace App\Models;

use App\Models\User;
use App\Models\Categories;
use App\Models\Suppliers;
use App\Models\Status;
use App\Models\Property;
use Illuminate\Database\Eloquent\Model;
use TarfinLabs\LaravelSpatial\Casts\LocationCast;
use TarfinLabs\LaravelSpatial\Traits\HasSpatial;

class Postulations extends Model
{
    //
    use HasSpatial;

    protected $fillable = [
        'user_id',
        'category_id',
        'property_id',
        'address',
        'int_number',
        'references',
        'appointment',
        'amount',
        'details',
        'photos',
        'status',
        'related_postulation_id',
        'status_id',
    ];

    protected $casts = [
        'location' => LocationCast::class,
        'photos'   => 'array',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function categories()
    {
        return $this->belongsTo('App\Models\Categories', 'category_id');
    }


    public function suppliers()
    {
        return $this->belongsToMany(Suppliers::class, 'postulations_suppliers', 'postulations_id', 'suppliers_id')
                    ->withPivot('date_1', 'date_2', 'amount')
                    ->withTimestamps();
    }

    public function property()
    {
        return $this->belongsTo(Property::class);
    }

    public function relatedPostulation()
    {
        return $this->belongsTo(Postulations::class, 'related_postulation_id');
    }

    public function status()
    {
        return $this->belongsTo(Status::class);
    }
}
