<?php

namespace App\Models;

use App\Libs\Database\Eloquent\ContentTypeModel;
use App\Models\GenericBackoffice\Traits\LanguageSupportTrait;
use Carbon\Carbon;

class EventItem extends ContentTypeModel
{
    use LanguageSupportTrait;

    protected $hidden = ['languages', "websites"];
    protected $with = ['languages', "websites"];

    public function image() {
        return $this->belongsTo(MediaItem::class);
    }

    public function topic() {
        return $this->belongsTo(Topic::class);
    }

    /**
     * Scope to filter upcoming events.
     * An event is upcoming if its end_date hasn't passed yet (or start_date if no end_date).
     */
    public function scopeUpcoming($query)
    {
        $now = Carbon::now('Europe/Brussels');

        $query->where(function ($q) use ($now) {
            $q->where(function ($q2) use ($now) {
                $q2->whereNotNull('end_date')
                    ->where('end_date', '>=', $now);
            })->orWhere(function ($q2) use ($now) {
                $q2->whereNull('end_date')
                    ->where('start_date', '>=', $now);
            });
        });
    }

    /**
     * Scope to filter past events.
     * An event is past if its end_date has passed (or start_date if no end_date).
     */
    public function scopePast($query)
    {
        $now = Carbon::now('Europe/Brussels');

        $query->where(function ($q) use ($now) {
            $q->where(function ($q2) use ($now) {
                $q2->whereNotNull('end_date')
                    ->where('end_date', '<', $now);
            })->orWhere(function ($q2) use ($now) {
                $q2->whereNull('end_date')
                    ->where('start_date', '<', $now);
            });
        });
    }
}
