# Yacht Dynamic Dropdowns Update

## Overview
This update makes the yacht charter dropdowns (Duration, Boat Size, Type) dynamically populate based on what's actually available in the database, rather than using hardcoded options.

## Changes Made

### 1. Database Changes

#### Migration: `0001_01_01_000220_add_hours_to_yachts_table.php` (Already existed)
- Adds `hours` column to `yachts` table to store duration (2, 3, or 5 hours)

#### Migration: `0001_01_01_000230_populate_yacht_hours.php` (NEW)
- Populates the `hours` field for all existing yachts
- Creates all combinations: 3 durations × 3 sizes × 3 types = 27 yachts per marina
- Previously: 9 yachts per marina (3 sizes × 3 types)
- Now: 27 yachts per marina (3 durations × 3 sizes × 3 types)

### 2. Backend Changes

#### `app/Models/Yacht.php`
- Added `hours` to fillable fields (already present)

#### `app/Http/Controllers/Api/YachtController.php`
- NEW: Added `options()` method
- Returns available yacht options dynamically based on marina/region
- Returns unique durations, sizes, types from database

#### `routes/api.php`
- NEW: Added `GET /api/yachts-options` endpoint

#### `app/Services/AIRecommendationService.php`
- Updated `yachtOptions()` method to include `hours` field in returned data
- Each yacht record now includes its duration

#### `app/Services/CostCalculatorService.php`
- Updated yacht cost calculation to use yacht's own `hours` field
- Keeps `YACHT_DURATION_FACTORS` constant for backward compatibility

#### `database/seeders/CatalogSeeder.php`
- Updated to seed all duration combinations
- Now creates 27 yachts per marina instead of 9

### 3. Frontend Changes

#### `resources/views/trip-planner/index.blade.php`

**Yacht Section Rendering:**
- Dropdowns now extract unique values from actual yacht data
- Duration dropdown: Shows only hours available in yacht records
- Boat Size dropdown: Shows only sizes available for selected duration
- Type dropdown: Shows only types available for selected duration + size

**Cascading Logic:**
1. User selects Duration → Size and Type reset to first available
2. User selects Size → Type resets to first available for that duration + size
3. User selects Type → Exact yacht match is found

**JavaScript Functions:**
- `setYachtOption(regionId, field, value)` - Updated to handle cascading selection
  - When duration changes: resets size and type to first available
  - When size changes: resets type to first available
  - When type changes: finds exact yacht match
  - Always updates `sel.yacht_id` to the matching yacht record

## How It Works

### Database Structure
Each yacht record now represents a specific combination:
```
Yacht ID: 1 → Duration: 2hrs, Size: 50ft, Type: Standard
Yacht ID: 2 → Duration: 2hrs, Size: 50ft, Type: Luxury
Yacht ID: 3 → Duration: 2hrs, Size: 50ft, Type: Ultra Luxury
Yacht ID: 4 → Duration: 2hrs, Size: 60ft, Type: Standard
... (27 total per marina)
```

### Frontend Flow
1. User enables yacht charter for an Emirate
2. Dropdowns show only options available in that Emirate's marinas
3. Selecting duration filters available sizes
4. Selecting size filters available types
5. The exact yacht ID matching all three criteria is stored
6. Price is calculated from that specific yacht's `price_per_hour × duration_factor`

## Migration Instructions

### For Fresh Installation
```bash
php artisan migrate
php artisan db:seed
```
The seeders will automatically create 27 yachts per marina.

### For Existing Database
```bash
# Run the new migration to populate hours field
php artisan migrate

# This will:
# 1. Delete existing 9 yachts per marina
# 2. Create 27 new yachts per marina (with all duration combinations)
```

**IMPORTANT:** This migration truncates and reseeds yacht data. Any manual changes to yacht records will be lost. If you have custom yacht data, export it first and reimport after migration.

### Safe Migration (Preserving Custom Data)
If you have custom yacht pricing or configurations:

```bash
# 1. Export current yacht data
php artisan tinker
>>> Yacht::all()->toJson()
# Copy the output

# 2. Run migration
php artisan migrate

# 3. Manually update custom pricing via admin panel
# Visit /admin/yachts to edit individual yacht records
```

## API Endpoints

### NEW: Get Yacht Options
```
GET /api/yachts-options?marina_id={id}
GET /api/yachts-options?region_id={id}
```

Response:
```json
{
  "durations": [2, 3, 5],
  "sizes": [50, 60, 70],
  "types": ["Standard", "Luxury", "Ultra Luxury"],
  "combinations": [
    {
      "yacht_id": 1,
      "duration": 2,
      "size": 50,
      "type": "Standard",
      "price_per_hour": 450,
      "operator": "Gunaydin Yacht Services"
    },
    ...
  ]
}
```

## Testing

### Test Dropdowns Work
1. Start trip planner: `http://localhost:8000`
2. Complete Step 1 (select Emirates with marinas: Dubai, Abu Dhabi, or RAK)
3. Complete Step 2 (interests)
4. In Step 3, enable yacht charter for an Emirate
5. Verify Duration dropdown shows: 2 Hrs, 3 Hrs, 5 Hrs
6. Select a duration
7. Verify Boat Size shows only available sizes for that duration
8. Select a size
9. Verify Type shows only available types for that duration + size
10. Price should update correctly

### Test Database
```bash
php artisan tinker
>>> use App\Models\Marina;
>>> use App\Models\Yacht;
>>> Marina::first()->yachts()->count()
# Should return 27 (3 durations × 3 sizes × 3 types)

>>> Yacht::where('marina_id', 1)->distinct('hours')->pluck('hours')
# Should return [2, 3, 5]

>>> Yacht::where('marina_id', 1)->where('hours', 2)->distinct('length_ft')->pluck('length_ft')
# Should return [50, 60, 70]
```

## Backward Compatibility

- API endpoints remain the same
- Frontend selection logic updated but maintains same data structure
- Cost calculation logic preserved
- Existing trip plans will continue to work

## Notes

- Marina count unchanged: 3 marinas (Dubai, Abu Dhabi, RAK)
- Total yacht records: 3 marinas × 27 combinations = 81 yacht records
- Duration factors still used for pricing: {2: 2.0, 3: 2.8, 5: 4.5}
- All yachts remain "Gunaydin Yacht Services" brand
