<?php

namespace App\Models;

use CodeIgniter\Model;

class WorkOrderStaffModel extends Model
{
    protected $table         = 'work_order_staff';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = false;

    protected $allowedFields = [
        'work_order_id', 'employee_id', 'role', 'notes', 'assigned_at', 'assigned_by',
    ];

    /**
     * @return list<array>
     */
    public function getForWorkOrder(int $workOrderId): array
    {
        return $this->select('work_order_staff.*, employees.full_name, employees.employee_number, employees.job_title')
            ->join('employees', 'employees.id = work_order_staff.employee_id', 'left')
            ->where('work_order_staff.work_order_id', $workOrderId)
            ->where('employees.deleted_at', null)
            ->orderBy('employees.full_name', 'ASC')
            ->findAll();
    }

    public function isAssigned(int $workOrderId, int $employeeId): bool
    {
        return $this->where('work_order_id', $workOrderId)
            ->where('employee_id', $employeeId)
            ->countAllResults() > 0;
    }
}
