<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\Pages\CourseController;
use App\Http\Controllers\Pages\CoachController;
use App\Http\Controllers\Pages\TrialBookingController;
use App\Http\Controllers\Pages\SpaController;
use App\Http\Controllers\Pages\SpaNewsController;
use App\Models\SpaCourse;
use App\Models\Venue;
use App\Support\Uploads\ImageVariants;
use Illuminate\Support\Facades\Storage;

Route::get('/', [HomeController::class, 'index'])->name('pages.home');

Route::view('/about', 'pages.about')->name('pages.about');

Route::get('/spa', [SpaController::class, 'index'])->name('pages.spa.index');

Route::get('/spa/news', [SpaNewsController::class, 'index'])->name('pages.spa.news.index');

Route::get('/spa/news/{slug}', [SpaNewsController::class, 'show'])->name('pages.spa.news.show');


Route::get('/spa/courses/{slug}', function (Request $request, string $slug) {
	$record = SpaCourse::query()
		->where('slug', $slug)
		->where('is_active', true)
		->firstOrFail();

	/** @var \Illuminate\Filesystem\FilesystemAdapter $publicDisk */
	$publicDisk = Storage::disk('public');

	$imageUrl = filled($record->main_image_path)
		? (ImageVariants::largeUrl($record->main_image_path, 'public') ?? $publicDisk->url($record->main_image_path))
		: asset('assets/img/about/collage-1.jpg');

	$duration = filled($record->duration_minutes)
		? ((string) $record->duration_minutes) . '分鐘'
		: '—';

	$price = '—';
	if (! is_null($record->price)) {
		$rawPrice = (string) $record->price;
		$priceFloat = (float) $rawPrice;
		$price = str_ends_with($rawPrice, '.00')
			? number_format($priceFloat, 0)
			: number_format($priceFloat, 2);
	}

	$course = [
		'slug' => $record->slug,
		'category' => $record->category === 'face' ? '臉部課程' : ($record->category === 'body' ? '身體課程' : '課程'),
		'title' => $record->name,
		'image' => $imageUrl,
		'short_description' => $record->short_description,
		'description' => $record->description,
		'duration' => $duration,
		'price_label' => '會員優惠價',
		'price' => $price,
	];

	$isPartial = $request->boolean('partial') || $request->ajax();
	if ($isPartial) {
		return view('pages.spa.courses._content', [
			'course' => $course,
		]);
	}

	return view('pages.spa.courses.show', [
		'course' => $course,
	]);
})->name('pages.spa.courses.show');

Route::get('/news', [\App\Http\Controllers\Pages\NewsController::class, 'index'])->name('pages.news.index');
Route::get('/news/{slug}', [\App\Http\Controllers\Pages\NewsController::class, 'show'])->name('pages.news.show');



Route::get('/facilities', function () {
	/** @var \Illuminate\Filesystem\FilesystemAdapter $publicDisk */
	$publicDisk = Storage::disk('public');

	$clubs = Venue::query()
		->where('is_active', true)
		->orderBy('sort_order')
		->get()
		->map(function (Venue $venue) use ($publicDisk): array {
			$heroImageUrl = filled($venue->hero_image_path)
				? (ImageVariants::mediumUrl($venue->hero_image_path, 'public') ?? $publicDisk->url($venue->hero_image_path))
				: null;

			$gallery = [];
			foreach (($venue->gallery_image_paths ?? []) as $path) {
				if (blank($path)) {
					continue;
				}
				$gallery[] = [
					'cat' => '相簿',
					'img' => ImageVariants::mediumUrl($path, 'public') ?? $publicDisk->url($path),
					'full' => ImageVariants::largeUrl($path, 'public') ?? $publicDisk->url($path),
				];
			}

			return [
				'slug' => $venue->slug,
				'title' => $venue->name,
				'venue_type' => $venue->venue_type,
				'cats' => array_values(array_filter([
					$venue->venue_type,
				])),
				'img' => $heroImageUrl,
				'address' => $venue->address,
				'phone' => $venue->phone,
				'hours' => $venue->open_hours,
				'parking' => $venue->transportation,
				'description' => $venue->summary,
				'gallery' => $gallery,
			];
		})
		->all();

	return view('pages.facilities.index', [
		'clubs' => $clubs,
	]);
})->name('pages.facilities.index');


Route::get('/facilities/{slug}', function (string $slug) {
	/** @var \Illuminate\Filesystem\FilesystemAdapter $publicDisk */
	$publicDisk = Storage::disk('public');

	$venue = Venue::query()
		->where('slug', $slug)
		->where('is_active', true)
		->firstOrFail();

	$venue->load([
		'facilities' => fn (\Illuminate\Database\Eloquent\Relations\BelongsToMany $query) =>
			$query
				->where('facilities.is_active', true)
				->wherePivot('is_active', true)
				->orderBy('facility_venue.sort_order')
				->orderBy('facilities.sort_order'),
	]);

	$heroImageUrl = filled($venue->hero_image_path)
		? (ImageVariants::mediumUrl($venue->hero_image_path, 'public') ?? $publicDisk->url($venue->hero_image_path))
		: null;

	$gallery = [];
	foreach ($venue->facilities as $facility) {
		$cat = $facility->name;

		foreach (($facility->gallery_image_paths ?? []) as $path) {
			if (blank($path)) {
				continue;
			}

			$gallery[] = [
				'cat' => $cat,
				'img' => ImageVariants::mediumUrl($path, 'public') ?? $publicDisk->url($path),
				'full' => ImageVariants::largeUrl($path, 'public') ?? $publicDisk->url($path),
			];
		}
	}

	$club = [
		'slug' => $venue->slug,
		'title' => $venue->name,
		'venue_type' => $venue->venue_type,
		'cats' => array_values(array_filter([
			$venue->venue_type,
		])),
		'img' => $heroImageUrl,
		'address' => $venue->address,
		'phone' => $venue->phone,
		'hours' => $venue->open_hours,
		'parking' => $venue->transportation,
		'description' => $venue->summary,
		'gallery' => $gallery,
	];

	return view('pages.facilities.show', [
		'club' => $club,
	]);
})->name('pages.facilities.show');

Route::view('/courses/week', 'pages.courses.week')->name('pages.courses.week');

Route::view('/free-trial', 'pages.free-trial')->name('pages.free-trial');

Route::post('/free-trial', [TrialBookingController::class, 'store'])->name('trial-bookings.store');

Route::get('/courses/{slug}', [CourseController::class, 'show'])->name('pages.courses.show');
Route::get('/coaches', [CoachController::class, 'index'])->name('pages.coaches.index');
Route::get('/coaches/{slug}', [CoachController::class, 'show'])->name('pages.coaches.show');
