# Dashboard Event Controller - Data Specification

## File: `Modules/Dashboard/app/Http/Controllers/EventController.php`

### Method: `index()`

Controller harus mengirimkan data berikut ke view `dashboard::event`:

```php
public function index(Request $request)
{
    $user = auth()->user();
    
    // Get events that user has registered for
    $events = \Modules\News\Models\Event::with(['registrations' => function($query) use ($user) {
            $query->where('user_id', $user->id);
        }])
        ->whereHas('registrations', function($query) use ($user) {
            $query->where('user_id', $user->id)
                  ->whereIn('status', ['registered', 'attended']);
        })
        ->where('status', 'published')
        ->orderBy('event_date', 'asc')
        ->get();
    
    return view('dashboard::event', [
        'title' => 'Event',
        'meta_description' => 'Daftar event yang Anda ikuti',
        'events' => $events,
    ]);
}
```

---

### Method: `detail($slug)`

Controller untuk menampilkan detail event:

```php
public function detail($slug)
{
    $user = auth()->user();
    
    // Get event detail
    $event = \Modules\News\Models\Event::with(['registrations' => function($query) use ($user) {
            $query->where('user_id', $user->id);
        }])
        ->where('slug', $slug)
        ->where('status', 'published')
        ->firstOrFail();
    
    // Check if user is registered
    $registration = $event->registrations->first();
    
    if (!$registration) {
        // Redirect back if user not registered for this event
        return redirect()->route('dashboard.event')->with('error', 'Anda belum terdaftar untuk event ini');
    }
    
    return view('dashboard::event-detail', [
        'title' => $event->title,
        'meta_description' => $event->description,
        'event' => $event,
        'registration' => $registration,
    ]);
}
```

---

## Data Variables Required

### 1. **$events** (Collection)
- **Source:** Table `events`
- **Relations:** 
  - `registrations` (HasMany event_registrations) - Filtered by current user
- **Filters:**
  - Has registration from current user
  - User registration status: 'registered' or 'attended'
  - Event status: 'published'
- **Order:** `event_date` ASC (upcoming events first)
- **Fields Used:**
  - `events.id` → unique identifier
  - `events.title` → event name/title
  - `events.slug` → for detail page URL
  - `events.featured_image` → event thumbnail/poster
  - `events.event_type` → 'online', 'offline', or 'hybrid'
  - `events.location` → physical location (if offline/hybrid)
  - `events.event_date` → event date (format: Y-m-d)
  - `events.start_time` → event start time (format: H:i:s)
  - `events.end_time` → event end time
- **Display:** Grid of event cards on event list page

### 2. **$event** (Model Object - for detail page)
- **Source:** Table `events`
- **Relations:** 
  - `registrations` (HasMany event_registrations)
- **Filters:**
  - `slug` = parameter from route
  - `status` = 'published'
- **Fields Used:**
  - All fields from list + additional:
  - `events.description` → short description
  - `events.content` → full event description/details
  - `events.platform` → online platform name (e.g., Zoom, Google Meet)
  - `events.platform_link` → meeting link
  - `events.max_participants` → maximum attendees
- **Display:** Event detail page with full information

### 3. **$registration** (Model Object - optional for detail page)
- **Source:** Table `event_registrations`
- **Purpose:** Check user's registration status for the event
- **Fields Used:**
  - `event_registrations.status` → 'registered', 'attended', 'cancelled', 'no_show'
  - `event_registrations.registered_at` → registration timestamp
  - `event_registrations.attended_at` → attendance timestamp (if attended)
- **Usage:** Can show registration status badge or attendance confirmation

---

## Database Tables Reference

### Related Tables:

1. **events** - Event calendar
   ```
   - id (BIGSERIAL)
   - title (VARCHAR)
   - slug (VARCHAR) - UNIQUE
   - description (TEXT)
   - content (TEXT) - Full details
   - featured_image (VARCHAR)
   - event_type (VARCHAR) - 'online', 'offline', 'hybrid'
   - location (VARCHAR)
   - platform (VARCHAR) - Zoom, Google Meet, etc.
   - platform_link (VARCHAR)
   - event_date (DATE) - UNIQUE (one event per date)
   - start_time (TIME)
   - end_time (TIME)
   - registration_deadline (TIMESTAMP)
   - max_participants (INTEGER)
   - reminder_days_before (INTEGER)
   - status (VARCHAR) - 'draft', 'published', 'cancelled', 'completed'
   - created_by (BIGINT) - Admin user ID
   - created_at (TIMESTAMP)
   - updated_at (TIMESTAMP)
   - deleted_at (TIMESTAMP)
   ```

2. **event_registrations** - User registrations
   ```
   - id (BIGSERIAL)
   - event_id (BIGINT)
   - user_id (BIGINT)
   - status (VARCHAR) - 'registered', 'attended', 'cancelled', 'no_show'
   - registered_at (TIMESTAMP)
   - attended_at (TIMESTAMP)
   - reminder_sent (BOOLEAN)
   - reminder_sent_at (TIMESTAMP)
   - day_reminder_sent (BOOLEAN)
   - day_reminder_sent_at (TIMESTAMP)
   - notes (TEXT)
   - UNIQUE(event_id, user_id)
   ```

---

## Route Definition

Add to `Modules/Dashboard/routes/web.php`:

```php
Route::middleware(['auth'])->prefix('dashboard')->name('dashboard.')->group(function () {
    Route::get('/event', [EventController::class, 'index'])->name('event');
    Route::get('/event/{slug}', [EventController::class, 'detail'])->name('event.detail');
});
```

---

## Sample Controller Implementation

```php
<?php

namespace Modules\Dashboard\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\News\Models\Event;

class EventController extends Controller
{
    public function index(Request $request)
    {
        $user = auth()->user();
        
        // Get events user has registered for
        $events = Event::with(['registrations' => function($query) use ($user) {
                $query->where('user_id', $user->id);
            }])
            ->whereHas('registrations', function($query) use ($user) {
                $query->where('user_id', $user->id)
                      ->whereIn('status', ['registered', 'attended']);
            })
            ->where('status', 'published')
            ->orderBy('event_date', 'asc')
            ->get();
        
        return view('dashboard::event', [
            'title' => 'Event',
            'meta_description' => 'Daftar event yang Anda ikuti',
            'events' => $events,
        ]);
    }
    
    public function detail($slug)
    {
        $user = auth()->user();
        
        // Get event with user's registration
        $event = Event::with(['registrations' => function($query) use ($user) {
                $query->where('user_id', $user->id);
            }])
            ->where('slug', $slug)
            ->where('status', 'published')
            ->firstOrFail();
        
        // Check if user registered
        $registration = $event->registrations->first();
        
        if (!$registration) {
            return redirect()->route('dashboard.event')
                ->with('error', 'Anda belum terdaftar untuk event ini');
        }
        
        return view('dashboard::event-detail', [
            'title' => $event->title,
            'meta_description' => $event->description,
            'event' => $event,
            'registration' => $registration,
        ]);
    }
}
```

---

## Model Relationships

### In `Modules\News\Models\Event.php`:

```php
public function registrations()
{
    return $this->hasMany(\Modules\News\Models\EventRegistration::class);
}

public function participants()
{
    return $this->belongsToMany(
        \App\Models\User::class,
        'event_registrations',
        'event_id',
        'user_id'
    )->withPivot('status', 'registered_at', 'attended_at');
}

public function creator()
{
    return $this->belongsTo(\App\Models\User::class, 'created_by');
}

// Helper methods
public function isOnline()
{
    return $this->event_type === 'online';
}

public function isOffline()
{
    return $this->event_type === 'offline';
}

public function isHybrid()
{
    return $this->event_type === 'hybrid';
}

public function isUpcoming()
{
    return $this->event_date > now()->toDateString();
}

public function isPast()
{
    return $this->event_date < now()->toDateString();
}

public function isToday()
{
    return $this->event_date === now()->toDateString();
}
```

### In `Modules\News\Models\EventRegistration.php`:

```php
public function event()
{
    return $this->belongsTo(\Modules\News\Models\Event::class);
}

public function user()
{
    return $this->belongsTo(\App\Models\User::class);
}

// Helper methods
public function isRegistered()
{
    return $this->status === 'registered';
}

public function hasAttended()
{
    return $this->status === 'attended';
}

public function isCancelled()
{
    return $this->status === 'cancelled';
}
```

---

## View Data Structure

### Event List Page Variables:
```php
[
    'title' => 'Event',
    'meta_description' => 'Daftar event yang Anda ikuti',
    'events' => Collection [
        Event {
            id: 1,
            title: 'INTRODUCTION ASQ V2',
            slug: 'introduction-asq-v2',
            featured_image: '/storage/events/intro-asq.jpg',
            event_type: 'online',
            location: null,
            event_date: '2025-12-20',
            start_time: '10:00:00',
            registrations: Collection [
                EventRegistration {
                    status: 'registered',
                    registered_at: '2025-12-01 09:00:00'
                }
            ]
        },
        // ... more events
    ]
]
```

### Event Detail Page Variables:
```php
[
    'title' => 'PELUNCURAN PRODUK ASQ',
    'meta_description' => 'Detail event peluncuran produk ASQ',
    'event' => Event {
        id: 1,
        title: 'PELUNCURAN PRODUK ASQ',
        slug: 'peluncuran-produk-asq',
        description: 'Event peluncuran produk terbaru',
        content: 'Acara ini menghadirkan pembaruan terbaru...',
        featured_image: '/storage/events/product-launch.jpg',
        event_type: 'online',
        location: null,
        platform: 'Zoom',
        platform_link: 'https://zoom.us/j/123456',
        event_date: '2025-12-19',
        start_time: '10:00:00',
        end_time: '12:00:00',
        max_participants: 100
    },
    'registration' => EventRegistration {
        status: 'registered',
        registered_at: '2025-12-01 09:00:00',
        attended_at: null
    }
]
```

---

## Notes for Backend Developer

1. **User Access:** Only show events that user has registered for
2. **Event Status:** Only display published events
3. **Ordering:** Show upcoming events first (order by event_date ASC)
4. **Detail Page Security:** Verify user registration before showing event details
5. **Image Handling:** Store event images in `storage/app/public/events/`
6. **Date Formatting:** Use Carbon for date formatting in views
7. **Registration Status:** Can add badges or indicators based on registration.status
8. **Platform Links:** Only show for online/hybrid events
9. **Reminder System:** Backend should handle email reminders based on reminder_days_before
10. **Empty State:** View handles empty events array with static sample data

---

## Additional Features (Optional)

1. **Attendance Confirmation:** Button to mark attendance (update status to 'attended')
2. **Calendar Export:** Download ICS file for calendar apps
3. **Share Event:** Social media sharing buttons
4. **Cancel Registration:** Allow users to cancel their registration
5. **Event Reminder:** Show countdown to event start time
6. **Past Events:** Separate tab for completed events with certificates/recordings

---

**Last Updated:** January 8, 2026
