<?php
/**
 * Event Model
 *
 * @package RadiusTheme\RadiusBooking\Models
 * @since   1.2.0
 */

namespace RadiusTheme\RadiusBooking\Models;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use RadiusTheme\RadiusBooking\Abstracts\BaseModel;

/**
 * Class Event
 *
 * @property int    $id
 * @property int    $organizer_id
 * @property string $name
 * @property string $slug
 * @property string $description
 * @property string $event_type
 * @property string $start_date
 * @property string $end_date
 * @property string $start_time
 * @property string $end_time
 * @property int    $capacity
 * @property int    $registered_count
 * @property bool   $waitlist_enabled
 * @property string $status
 */
class Event extends BaseModel {

	protected string $table = 'radius_booking_events';

	protected bool $softDeletes = true;

	protected array $fillable = array(
		'organizer_id',
		'name',
		'slug',
		'description',
		'short_description',
		'event_type',
		'start_date',
		'end_date',
		'start_time',
		'end_time',
		'timezone',
		'location_type',
		'location_value',
		'category_id',
		'capacity',
		'registered_count',
		'waitlist_enabled',
		'waitlist_capacity',
		'status',
		'registration_deadline',
		'recurring_pattern',
		'settings',
		'picture_full_path',
		'picture_thumb_path',
		'is_featured',
	);

	protected array $casts = array(
		'organizer_id'     => 'integer',
		'category_id'      => 'integer',
		'capacity'         => 'integer',
		'registered_count' => 'integer',
		'waitlist_enabled' => 'boolean',
		'waitlist_capacity' => 'integer',
		'is_featured'      => 'boolean',
		'recurring_pattern' => 'json',
		'settings'         => 'json',
	);

	public function organizer() {
		return $this->belongsTo( User::class, 'organizer_id' );
	}

	public function ticketTypes() {
		return $this->hasMany( EventTicketType::class, 'event_id', 'id' );
	}

	public function registrations() {
		return $this->hasMany( EventRegistration::class, 'event_id', 'id' );
	}

	public function sessions() {
		return $this->hasMany( EventSession::class, 'event_id', 'id' );
	}

	public function category() {
		return $this->belongsTo( Category::class, 'category_id' );
	}

	protected function onCreating(): void {
		if ( empty( $this->slug ) ) {
			$this->slug = sanitize_title( $this->name ) . '-' . wp_generate_password( 6, false, false );
		}
		if ( empty( $this->timezone ) ) {
			$this->timezone = wp_timezone_string();
		}
	}

	/**
	 * Flag the event as cancelled so the status change is persisted in the
	 * same UPDATE that sets deleted_at (the soft-delete scope would exclude
	 * the row from a follow-up query once deleted_at is written).
	 */
	protected function onDeleting(): void {
		if ( 'cancelled' !== $this->getAttribute( 'status' ) ) {
			$this->setAttribute( 'status', 'cancelled' );
		}
	}

	/**
	 * Cascade to related registrations after the event is soft-deleted.
	 * Registrations are flipped to status='cancelled' but NOT soft-deleted,
	 * so refund reports and audit history remain queryable.
	 */
	protected function onDeleted(): void {
		$event_id = $this->getKey();
		if ( ! $event_id ) {
			return;
		}

		EventRegistration::where( 'event_id', $event_id )
			->where( 'status', '!=', 'cancelled' )
			->update( array( 'status' => 'cancelled' ) );

		do_action( 'radius_booking_event_cancelled', $this );
	}

	public function get_id() {
		return $this->attributes['id'] ?? null;
	}

	public function get_name() {
		return $this->attributes['name'] ?? null;
	}

	public function get_slug() {
		return $this->attributes['slug'] ?? null;
	}
}
