<?php
/**
 * Class AppointmentsTable
 *
 * Manages the creation and deletion of the appointments table. This table is used
 * to store appointment details such as user, service, date, time, status, and related
 * notes. It also includes foreign key relationships, indexes for query optimization,
 * and soft delete functionality.
 */

namespace RadiusTheme\RadiusBooking\Databases\Table;

use RadiusTheme\RadiusBooking\Core\Database\Schema\Blueprint;
use RadiusTheme\RadiusBooking\Abstracts\Migration;
use RadiusTheme\RadiusBooking\Core\Database\Schema\Schema;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/**
 * Handles the creation and deletion of the 'appointments' table in the database.
 * Defines columns for storing appointment details such as user and service references,
 * status, timestamps, and related indexes to optimize query performance. Establishes
 * and enforces foreign key relationships with related tables.
 */
class AppointmentsTable extends Migration {

	/**
	 * Creates the 'appointments' table with columns for appointment details, including user and service references,
	 * status, notes, timestamps, and soft deletes. Includes indexes for optimized queries and sets up foreign key relationships.
	 *
	 * @return void
	 */
	public function up(): void {
		Schema::create(
			$this->tablePrefix . 'appointments',
			function ( Blueprint $table ) {
				$table->id();
				$table->unsignedBigInteger( 'employee_id' );
				$table->unsignedBigInteger( 'service_id' );
				$table->date( 'appointment_date' );
				$table->time( 'start_time' );
				$table->time( 'end_time' );
				$table->enum( 'status', array( 'pending', 'confirmed', 'completed', 'cancelled' ) )
					->default( 'pending' );
				$table->unsignedInteger( 'location_id' )->nullable();
				$table->boolean( 'notify_customer' )->default( true );
				$table->text( 'notes' )->nullable();
				$table->text( 'cancellation_reason' )->nullable();
				$table->unsignedInteger( 'parent_id' )->nullable();
				$table->string( 'booking_type', 50 )->default( 'service_booking' );
				$table->unsignedBigInteger( 'meeting_type_id' )->nullable();
				$table->unsignedBigInteger( 'event_id' )->nullable();
				$table->timestamps();
				$table->softDeletes();

				$table->index( array( 'employee_id' ) );
				$table->index( array( 'service_id' ) );
				$table->index( 'status' );
				$table->index( 'appointment_date' );
				$table->index( 'booking_type' );
				$table->index( 'meeting_type_id' );
				$table->index( 'event_id' );
			}
		);
	}

	/**
	 * Drops the appointments table.
	 *
	 * @return void
	 */
	public function down(): void {
		Schema::drop( $this->tablePrefix . 'appointments' );
	}
}
