<?php
/**
 * Migration class responsible for managing the creation and deletion
 * of the 'services' table in the database.
 */

namespace RadiusTheme\RadiusBooking\Databases\Table;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
use RadiusTheme\RadiusBooking\Core\Database\Schema\Blueprint;
use RadiusTheme\RadiusBooking\Abstracts\Migration;
use RadiusTheme\RadiusBooking\Core\Database\Schema\Schema;

/**
 * Migration class responsible for managing the creation and deletion
 * of the 'services' table in the database.
 */
class ServicesTable extends Migration {
	/**
	 * Creates the 'services' table with specific columns and indexes.
	 *
	 * @return void
	 */
	public function up(): void {
		Schema::create(
			$this->tablePrefix . 'services',
			function ( Blueprint $table ) {
				$table->id(); // AUTO_INCREMENT PRIMARY KEY
				$table->string( 'name' )->default( '' );
				$table->text( 'description' )->nullable();
				$table->string( 'color' )->default( '' );
				$table->decimal( 'price', 10, 2 );
				$table->enum( 'status', array( 'hidden', 'visible', 'disabled' ) )->default( 'visible' );
				$table->unsignedInteger( 'category_id' );
				$table->unsignedInteger( 'min_capacity' );
				$table->unsignedInteger( 'max_capacity' );
				$table->unsignedInteger( 'duration' )->comment( 'Duration in minutes' );
				$table->unsignedInteger( 'time_before' )->nullable()->default( 0 );
				$table->unsignedInteger( 'time_after' )->nullable()->default( 0 );
				$table->boolean( 'bringing_anyone' )->nullable()->default( 1 );
				$table->enum( 'priority', array( 'least_expensive', 'most_expensive', 'least_occupied', 'most_occupied' ) );
				$table->string( 'picture_full_path' )->nullable();
				$table->string( 'picture_thumb_path' )->nullable();
				$table->unsignedInteger( 'position' )->default( 0 );
				$table->boolean( 'is_visible' )->default( 1 );
				$table->boolean( 'aggregated_price' )->default( 1 );
				$table->json( 'settings' )->nullable()->default( null );
				$table->enum( 'recurring_cycle', array( 'disabled', 'all', 'daily', 'weekly', 'monthly', 'yearly' ) )->default( 'disabled' );
				$table->enum( 'recurring_sub', array( 'disabled', 'past', 'future', 'both' ) )->default( 'future' );
				$table->unsignedInteger( 'recurring_payment' )->default( 0 );
				$table->boolean( 'deposit_payment' )->default( 0 );
				$table->enum( 'deposit_type', array( 'fixed', 'percentage' ) )->default( 'fixed' );
				$table->boolean( 'deposit_per_person' )->default( 0 );
				$table->decimal( 'deposit', 10, 2 )->default( 0 );
				$table->boolean( 'full_payment' )->default( 0 );
				$table->boolean( 'mandatory_extra' )->default( 0 );
				$table->unsignedInteger( 'min_selected_extras' )->nullable()->default( 0 );
				$table->text( 'custom_pricing' )->nullable()->default( null );
				$table->unsignedInteger( 'max_extra_people' )->nullable()->default( null );
				$table->json( 'limit_per_customer' )->nullable()->default( null );
				$table->json( 'translations' )->nullable()->default( null );
				$table->unsignedBigInteger( 'listing_id' )->nullable()->default( null );
				$table->string( 'source_type', 50 )->default( 'manual' );
				$table->timestamps();
				// Indexes
				$table->index( 'status' );
				$table->index( 'category_id' );
				$table->index( 'is_visible' );
				$table->index( 'listing_id' );
			}
		);
	}

	/**
	 * Reverses the migration by dropping the 'services' table.
	 *
	 * @return void
	 */
	public function down(): void {
		Schema::drop( $this->tablePrefix . 'services' );
	}
}
