<?php

namespace Modules\MedWorkforce\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * One staff-member × date × shift cell on the duty-roster board (SIFA P7-T1
 * staff_scheduling). user_id / swap_with_user_id / created_by reference the ERP
 * core `users` (App\User) — the same staff Essentials attendance tracks.
 */
class RosterAssignment extends Model
{
    protected $table = 'medwf_roster_assignments';

    protected $guarded = ['id'];

    protected $casts = [
        'date' => 'date',
    ];

    public const STATUSES = ['scheduled', 'confirmed', 'swapped', 'cancelled', 'no_show'];

    public function template()
    {
        return $this->belongsTo(ShiftTemplate::class, 'shift_template_id');
    }

    public function user()
    {
        return $this->belongsTo(\App\User::class, 'user_id');
    }

    public function swapWithUser()
    {
        return $this->belongsTo(\App\User::class, 'swap_with_user_id');
    }

    public function swapRequests()
    {
        return $this->hasMany(SwapRequest::class, 'roster_assignment_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function scopeBetween($query, $from, $to)
    {
        return $query->whereBetween('date', [$from, $to]);
    }

    public function scopeActive($query)
    {
        return $query->whereNotIn('status', ['cancelled']);
    }
}
