<?php

namespace App\Http\Controllers;

use App\Services\EquipmentInspectionService;
use App\Services\EquipmentRepairService;
use App\Services\EquipmentService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class EquipmentController extends Controller
{
    public function __construct(
        private EquipmentService $equipmentService,
        private EquipmentInspectionService $inspectionService,
        private EquipmentRepairService $repairService
    ) {}

    public function dashboard(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('equipment.dashboard'));
        }

        $stats = $this->equipmentService->getDashboardStats();
        $typeStats = $this->equipmentService->getTypeStats();
        $inspectionStats = $this->inspectionService->getMonthlyStats(now()->format('Y-m'));
        $recentRepairs = $this->repairService->getRecentRepairs(5);

        return view('equipment.dashboard', compact('stats', 'typeStats', 'inspectionStats', 'recentRepairs'));
    }

    public function index(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('equipment.index'));
        }

        return view('equipment.index');
    }

    public function create(): View
    {
        $users = \App\Models\User::orderBy('name')->get(['id', 'name']);

        return view('equipment.create', compact('users'));
    }

    public function show(int $id): View
    {
        $equipment = $this->equipmentService->getEquipmentById($id);

        if (! $equipment) {
            abort(404, '설비를 찾을 수 없습니다.');
        }

        return view('equipment.show', compact('equipment'));
    }

    public function edit(int $id): View
    {
        $users = \App\Models\User::orderBy('name')->get(['id', 'name']);

        return view('equipment.edit', compact('id', 'users'));
    }

    public function inspections(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('equipment.inspections'));
        }

        $equipmentList = $this->equipmentService->getEquipmentList();

        return view('equipment.inspections.index', compact('equipmentList'));
    }

    public function repairs(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('equipment.repairs'));
        }

        $equipmentList = $this->equipmentService->getEquipmentList();

        return view('equipment.repairs.index', compact('equipmentList'));
    }

    public function repairCreate(): View
    {
        $equipmentList = $this->equipmentService->getEquipmentList();
        $users = \App\Models\User::orderBy('name')->get(['id', 'name']);

        return view('equipment.repairs.create', compact('equipmentList', 'users'));
    }

    public function import(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('equipment.import'));
        }

        return view('equipment.import');
    }

    public function guide(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('equipment.guide'));
        }

        return view('equipment.guide');
    }
}
