<?php

namespace App\Modules\HR\Training\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Employees\Repositories\EmployeeRepository;
use App\Modules\HR\Training\Models\TrainingCourse;
use App\Modules\HR\Training\Models\TrainingSession;
use App\Modules\HR\Training\Repositories\TrainingRepository;
use App\Modules\HR\Training\Requests\TrainingRequest;
use App\Modules\HR\Training\Services\TrainingService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class TrainingController extends Controller
{
    public function __construct(
        private readonly TrainingRepository $repository,
        private readonly TrainingService $service,
        private readonly EmployeeRepository $employees,
    ) {}

    public function courses(): JsonResponse|View
    {
        $courses = $this->repository->courses((int) auth()->user()->tenant_id);

        return request()->expectsJson() ? response()->json($courses) : view('pages.hr.training.courses.index', compact('courses'));
    }

    public function storeCourse(TrainingRequest $r): JsonResponse|RedirectResponse
    {
        $course = $this->repository->saveCourse((int) $r->user()->tenant_id, $r->validated());

        return $r->expectsJson() ? response()->json($course, 201) : back()->with('success', __('training.flash.created'));
    }

    public function updateCourse(TrainingRequest $r, TrainingCourse $course): JsonResponse|RedirectResponse
    {
        $course = $this->repository->saveCourse((int) $course->tenant_id, $r->validated(), $course);

        return $r->expectsJson() ? response()->json($course) : back()->with('success', __('training.flash.updated'));
    }

    public function sessions(): JsonResponse|View
    {
        $tenant = (int) auth()->user()->tenant_id;
        $sessions = $this->repository->sessions($tenant);
        if (request()->expectsJson()) {
            return response()->json($sessions);
        }

        return view('pages.hr.training.sessions.index', [
            'sessions' => $sessions, 'courses' => $this->repository->courseOptions($tenant),
            'employees' => $this->employees->listForSelect($tenant),
        ]);
    }

    public function storeSession(TrainingRequest $r, TrainingCourse $course): JsonResponse|RedirectResponse
    {
        $session = $this->repository->saveSession((int) $course->tenant_id, $r->validated() + ['course_id' => $course->id]);

        return $r->expectsJson() ? response()->json($session, 201) : back()->with('success', __('training.flash.created'));
    }

    public function updateSession(TrainingRequest $r, TrainingSession $session): JsonResponse|RedirectResponse
    {
        $session = $this->repository->saveSession((int) $session->tenant_id, $r->validated(), $session);

        return $r->expectsJson() ? response()->json($session) : back()->with('success', __('training.flash.updated'));
    }

    public function destroyCourse(TrainingCourse $course): JsonResponse|RedirectResponse
    {
        $this->repository->deleteCourse($course);

        return request()->expectsJson() ? response()->json([], 204) : back()->with('success', __('training.flash.deleted'));
    }

    public function destroySession(TrainingSession $session): JsonResponse|RedirectResponse
    {
        $this->repository->deleteSession($session);

        return request()->expectsJson() ? response()->json([], 204) : back()->with('success', __('training.flash.deleted'));
    }

    public function enroll(TrainingRequest $r, TrainingSession $session): JsonResponse|RedirectResponse
    {
        $this->service->enroll($session, $r->validated('employee_ids'));

        return $r->expectsJson()
            ? response()->json(['enrolled' => true])
            : back()->with('success', __('training.flash.enrolled'));
    }
}
