<?php

namespace App\Http\Controllers\HR;

use App\Http\Controllers\Controller;
use App\Models\HR\Attendance;
use App\Models\HR\Leave;
use App\Services\HR\AttendanceService;
use App\Services\HR\LeaveService;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class AttendanceIntegratedController extends Controller
{
    public function __construct(
        private AttendanceService $attendanceService,
        private LeaveService $leaveService
    ) {}

    /**
     * 근태관리 통합 화면
     */
    public function index(Request $request): View|Response
    {
        // JS 필요 페이지 → HTMX 시 전체 리로드
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('hr.attendance.index'));
        }

        $stats = $this->attendanceService->getMonthlyStats();
        $departments = $this->leaveService->getDepartments();
        $employees = $this->leaveService->getActiveEmployees();
        $statusMap = Attendance::STATUS_MAP;
        $leaveTypeMap = Leave::TYPE_MAP;
        $leaveStatusMap = Leave::STATUS_MAP;

        // 결재선 목록
        $approvalLines = \App\Models\Approvals\ApprovalLine::query()
            ->where('tenant_id', session('selected_tenant_id'))
            ->orderBy('name')
            ->get(['id', 'name', 'is_default']);

        return view('hr.attendance-integrated.index', [
            'stats' => $stats,
            'departments' => $departments,
            'employees' => $employees,
            'statusMap' => $statusMap,
            'leaveTypeMap' => $leaveTypeMap,
            'leaveStatusMap' => $leaveStatusMap,
            'approvalLines' => $approvalLines,
        ]);
    }
}
