<?php

namespace App\Models;

use Config\Lookups;

/**
 * Employment documents attached to a staff profile — see
 * CreateStaffDocumentsTable's migration comment for why these are
 * downloaded through an authenticated UserController action rather than
 * the public UploadController::serve() route staff photos use.
 */
class StaffDocumentModel extends TenantModel
{
    protected $table            = 'staff_documents';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useTimestamps    = true;
    protected $createdField     = 'created_at';
    protected $updatedField     = '';

    protected $allowedFields = [
        'user_id',
        'document_type',
        'file_path',
        'original_filename',
        'uploaded_by',
    ];

    public function __construct()
    {
        parent::__construct();

        $types = array_keys((new Lookups())->staffDocumentTypes);

        $this->validationRules = [
            'user_id'       => 'required|integer',
            'document_type' => 'required|in_list[' . implode(',', $types) . ']',
        ];
    }

    public function forUser(int $userId): array
    {
        return $this->where('user_id', $userId)->orderBy('created_at', 'desc')->findAll();
    }
}
