<?php

namespace App\Models;

use App\Models\Worker;
use App\Models\Projects;

use App\Models\CompanyWorkerTimesheet; 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class ProjectSupervisors extends Model
{
    //** Traits */
    use HasFactory;
    use SoftDeletes;

    //** Variables */

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

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

    /**
     * Define attributes that are mass assignable. 
     * 
     * @var array 
     */
    protected $fillable =
    [
        'user_id',
        'project_id',
        'date_from',
        'date_to',
    ];

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

    //** Accessors & Mutators */

    //...

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

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

    /**
     * Fetch timesheets hasMany relationship. 
     * 
     * // @return ? 
     */
    public function timesheets() // : ? 
    {
        //! Should be user_id -> worker_id & reference_id->project_id
        //! Best solution would be to revamp table in the future
        return $this->hasMany(
            related: CompanyWorkerTimesheet::class,
            foreignKey: "reference_id",
            localKey: "project_id",
        );
    }

    public function worker()
    {
        return $this->belongsTo(Worker::class, 'user_id', 'id');
    }

}
