<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Presensi;
use App\Models\Cuti;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;

class DashboardController extends Controller
{
    public function getUserStats($userId)
    {
        try {
            // Pastikan user hanya bisa melihat data statistiknya sendiri
            if (Auth::id() != $userId) {
                return response()->json([
                    'success' => false,
                    'message' => 'Anda tidak memiliki akses untuk melihat data ini'
                ], 403);
            }

            $user = User::findOrFail($userId);
            $currentMonth = Carbon::now()->month;
            $currentYear = Carbon::now()->year;

            // Hitung jumlah kehadiran bulan ini
            $attendanceCount = Presensi::where('user_id', $userId)
                ->whereMonth('created_at', $currentMonth)
                ->whereYear('created_at', $currentYear)
                ->where('status', 'Hadir')
                ->count();

            // Hitung sisa cuti
            $totalLeaveTaken = Cuti::where('user_id', $userId)
                ->where('status', 'Disetujui')
                ->whereYear('tanggal_mulai', $currentYear)
                ->count();

            // Asumsi total cuti per tahun adalah 12 hari
            $remainingLeave = 12 - $totalLeaveTaken;
            $remainingLeave = max(0, $remainingLeave); // Pastikan tidak negatif

            return response()->json([
                'success' => true,
                'data' => [
                    'user_name' => $user->name,
                    'attendance_count' => $attendanceCount,
                    'remaining_leave' => $remainingLeave,
                    'month' => Carbon::now()->format('F Y')
                ]
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal mengambil data statistik: ' . $e->getMessage()
            ], 500);
        }
    }

    public function getStats(Request $request)
    {
        try {
            // Ambil user_id dari request
            $userId = $request->input('user_id');
            
            if (!$userId) {
                $userId = Auth::id(); // Gunakan user yang login jika tidak ada parameter
            }

            // Validasi user
            $user = User::findOrFail($userId);
            
            $currentMonth = Carbon::now()->month;
            $currentYear = Carbon::now()->year;

            // Hitung total presensi user
            $totalPresensi = Presensi::where('user_id', $userId)
                ->whereYear('created_at', $currentYear)
                ->count();

            // Hitung total cuti user
            $totalCuti = Cuti::where('user_id', $userId)
                ->count();

            return response()->json([
                'success' => true,
                'data' => [
                    'user_name' => $user->name,
                    'total_presensi' => $totalPresensi,
                    'total_cuti' => $totalCuti,
                    'year' => $currentYear
                ]
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal mengambil data statistik: ' . $e->getMessage()
            ], 500);
        }
    }
} 