<?php

namespace App\Http\Controllers\Controllers;

use App\Models\User;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Http\Controllers\Controller;

class LandingPageController extends Controller
{
    public function index()
    {
        $employees = User::where('status', 'aktif')
            ->with(['attendances' => function ($q) {
                $q->today();
            }])
            ->get()
            ->map(function ($user) {
                $today = $user->todayAttendance();
                return [
                    'id'      => $user->id,
                    'name'    => $user->name,
                    'role'    => $user->role,
                    'phone'   => $user->phone,
                    'date'    => Carbon::today()->format('d F Y'),
                    'checkin' => $today && $today->check_in
                        ? Carbon::parse($today->check_in)->format('H:i')
                        : '-',
                    'checkout' => $today && $today->check_out
                        ? Carbon::parse($today->check_out)->format('H:i')
                        : '-',
                    'status'  => $today ? $today->status : '',
                ];
            });

        // Get news categories for dynamic loading
        $newsCategories = News::published()
            ->select('category')
            ->distinct()
            ->pluck('category');

        return view('welcome', [ // sesuaikan nama view landingmu
            'members' => $employees,
            'newsCategories' => $newsCategories,
        ]);
    }
}
