<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Absensi;
use Illuminate\Http\Request;

class AbsensiController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();
        $santri = $user->santri;

        if (!$santri) {
            return response()->json([
                'success' => false,
                'message' => 'Santri tidak ditemukan untuk user ini',
                'data' => [],
            ], 404);
        }

        $absensi = Absensi::where('santri_id', $santri->id)
            ->orderBy('tanggal', 'desc')
            ->get()
            ->map(function ($item) {
                return [
                    'id' => $item->id,
                    'santri_id' => $item->santri_id,
                    'kelas_id' => $item->kelas_id,
                    'tanggal' => $item->tanggal,
                    'status' => $item->status,
                    'keterangan' => $item->keterangan,
                    'created_at' => $item->created_at,
                    'updated_at' => $item->updated_at,
                ];
            });

        return response()->json([
            'success' => true,
            'message' => 'Data absensi berhasil diambil',
            'data' => $absensi,
        ]);
    }
}
