<?php

namespace App\Modules\HR\EmployeeSelfService\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\EmployeeSelfService\Repositories\SelfServicePortalRepository;
use App\Modules\HR\EmployeeSelfService\Services\SelfEmployeeContextService;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class MePortalResourcesController extends Controller
{
    public function __construct(
        private readonly SelfEmployeeContextService $context,
        private readonly SelfServicePortalRepository $resources,
    ) {}

    public function documents(): View
    {
        $employee = $this->context->employee();

        return view('pages.me.documents', [
            'documents' => $this->resources->documents((int) $employee->id),
        ]);
    }

    public function downloadDocument(int $document): BinaryFileResponse
    {
        $employee = $this->context->employee();
        $row = $this->resources->document((int) $employee->id, $document);
        abort_if($row === null || ! Storage::disk('local')->exists($row->file_path), 404);

        return Storage::disk('local')->download($row->file_path, basename($row->file_path));
    }

    public function training(): View
    {
        $employee = $this->context->employee();

        return view('pages.me.training', [
            'enrollments' => $this->resources->training((int) $employee->tenant_id, (int) $employee->id),
        ]);
    }

    public function assets(): View
    {
        $employee = $this->context->employee();

        return view('pages.me.assets', [
            'assets' => $this->resources->assets((int) $employee->tenant_id, (int) $employee->id),
        ]);
    }
}
