<?php

namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Models\Event;
use App\Models\Notification;
use Carbon\Carbon; // Add this line
use Illuminate\Support\Facades\Auth; 
use App\Models\Client;

class ClientEventsController extends Controller
{
    public function index()
    {
        // Get the authenticated user
        $user = Auth::user();
        $client_id = $user->client_id; 
    
        // Retrieve events for the authenticated client's client_id, ordered by event_date in descending order
        $events = Event::where('client_name', $client_id)
                       ->orderBy('event_date', 'desc')
                       ->get();
        
        // Retrieve all clients (if necessary, depending on your use case)
        $clients = Client::all();
        
        // Return the view with the filtered events and clients
        return view('client.events.index', compact('events', 'clients'));
    }
    

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'client_name' => 'nullable|string|max:255',
            'event_date' => 'required|date',
            'event_time' => 'required|date_format:H:i',
        ]);

        $event = Event::create($request->all());

        $this->createNotification($event);

        return redirect()->route('client.events.index')->with('success', 'Event added successfully!');
    }

    public function update(Request $request)
    {
        $request->validate([
            'id' => 'required|exists:events,id',
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'client_name' => 'nullable|string|max:255',
            'event_date' => 'required|date',
            'event_time' => 'required|date_format:H:i',
        ]);

        $event = Event::findOrFail($request->id);
        $event->update($request->all());

        $this->createNotification($event);

        return redirect()->route('client.events.index')->with('success', 'Event updated successfully!');
    }

    public function events()
    {
        $user = Auth::user();
        $client_id = $user->client_id; 
    
        // Retrieve events for the authenticated client's client_id, ordered by event_date in descending order
        $events = Event::where('client_name', $client_id)
                       ->orderBy('event_date', 'desc')
                       ->get();
        $eventsFormatted = [];

        foreach ($events as $event) {
            $eventsFormatted[] = [
                'id' => $event->id,
                'title' => $event->title,
                'start' => $event->event_date . 'T' . $event->event_time,
                'description' => $event->description,
                'client_name' => $event->client_name,
            ];
        }

        return response()->json($eventsFormatted);
    }

    protected function createNotification(Event $event)
    {
        $eventDateTime = Carbon::createFromFormat('Y-m-d H:i', $event->event_date . ' ' . $event->event_time);
        $notificationTime = $eventDateTime->subMinutes(30);

        $notification = Notification::create([
            'title' => 'Upcoming Event',
            'body' => 'The event "' . $event->title . '" is happening in 30 minutes.',
            'send_at' => $notificationTime,
        ]);

        \Log::info('Notification created:', $notification->toArray());
    }
}
