<?php

namespace App\Models;

use App\Models\Company; 
use App\Models\ZipcodeRange;
use App\Models\Expertise;
use App\Models\WorkAreas; 
use App\Models\WorkerRequestApplication; 

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class WorkerRequest extends Model
{
	//** Traits */
    use HasFactory;

	//** Variables */

	/**
	 * Manually Define Table Name.  
	 * 
	 * @var string 
	 */
    protected $table = 'worker_requests';

	/**
	 * Define Relationships that must always be loaded. 
	 * 
	 * @var array 
	 */
	protected $with = 
	[
		//...
	];

	/**
	 * Define attributes that are mass assignable. 
	 * 
	 * @var array 
	 */
    protected $fillable = 
	[
		'expertise_id',
		'experience',
		'status',
		'zipcode_range_id',
		'date_start',
		'date_end',
        'company_id',
		'description',
		'zip_code_radius',
		'on_success_action',
		'number_of_applicants_to_be_borrowed',
		'number_of_applicants_successfully_borrowed'
    ];

	/**
	 * Define attributes that should be hidden for arrays. 
	 * 
	 * @var array 
	 */
    protected $hidden = 
	[
		//...
    ];

	//** Accessors & Mutators */

	//...

	//** belongsTo, belongsToMany, hasOne, hasMany relationships */ 

	/**
	 * Fetch Company belongsTo relationship. 
	 * 
	 * // @return ? 
	 */
	public function company() // : ? 
	{
		return $this->belongsTo(
			related   : Company::class,
			foreignKey: "company_id",
			ownerKey  : "id",
			relation  : "company" 
		);
	}

	/**
	 * Fetch Expertise belongsTo relationship. 
	 * 
	 * // @return ? 
	 */
	public function expertise() // : ? 
	{
		return $this->belongsTo(
			related   : Expertise::class,
			foreignKey: "expertise_id",
			ownerKey  : "id",
			relation  : "expertise" 
		);
	}


	/**
	 * Fetch ZipcodeRange belongsTo relationship. 
	 * 
	 * // @return ? 
	 */
	public function zipCodeRange() // : ? 
	{
		return $this->belongsTo(
			related   : ZipcodeRange::class,
			foreignKey: "zipcode_range_id",
			ownerKey  : "id", 
			relation  : "zipCodeRange" 
		);
	}

	/**
	 * Fetch workAreas belongsToMany relationship via pivot table. 
	 * 
	 * // @return ? 
	 */
	public function workAreas() // : ? 
	{
		return $this->belongsToMany(
			related        : WorkAreas::class,
			table          : "worker_request_work_areas",
			foreignPivotKey: "worker_request_id",
			relatedPivotKey: "work_area_id",
			parentKey      : "id",
			relatedKey     : "id", 
			relation       : "workAreas"
		);
	}

	/**
	 * Fetch applicantions hasMany relationship. 
	 * 
	 * // @return ? 
	 */
	public function applications() // : ? 
	{
		return $this->hasMany(
			related		: WorkerRequestApplication::class, 
			foreignKey  : "worker_request_id", 
			localKey	: "id",
		);
	}

	/**
	 * Fetch workAreas belongsToMany relationship via pivot table. 
	 * 
	 * // @return ? 
	 */
	public function worker_request_work_areas() // : ? 
	{
		return $this->belongsToMany(
			related: WorkAreas::class,
			table: "worker_request_work_areas",
			foreignPivotKey: "worker_request_id",
			relatedPivotKey: "work_area_id",
			parentKey: "id",
			relatedKey: "id",
			relation: "workAreas"
		);
	}

	/**
	 * Fetch applicantions hasMany relationship. 
	 * 
	 * // @return ? 
	 */
	public function worker_request_applications() // : ? 
	{
		return $this->hasMany(
			related: WorkerRequestApplication::class,
			foreignKey: "worker_request_id",
			localKey: "id",
		);
	}
}