# Trip Reports Backend Implementation

This document outlines the backend implementation for trip reports functionality.

## Overview

The trip reports feature allows users to create reports about their hiking trips, including multiple peaks, descriptions, dates, and visibility settings.

## Database Structure

### Tables

1. **trip_reports** - Main table for trip reports
   - `id` - Primary key
   - `user_id` - Foreign key to users table
   - `description` - Text description of the trip (nullable)
   - `start_date` - Start date in DD/MM/YYYY format
   - `end_date` - End date in DD/MM/YYYY format (nullable)
   - `visibility` - Enum: 'private', 'friends', 'public'
   - `timestamps` - created_at, updated_at

2. **logs** table (updated) - Now includes optional relationship to trip reports
   - Existing columns: `id`, `user_id`, `mountain_id`, `done`, `date_climbed`, `description`, `timestamps`
   - **New column**: `trip_report_id` - Foreign key to trip_reports table (nullable)
   
   When a log has a `trip_report_id`, it's part of a trip report. Logs without a `trip_report_id` are standalone logs.

## Models

### TripReport Model
Located at: `app/Models/TripReport.php`

**Relationships:**
- `user()` - BelongsTo User
- `logs()` - HasMany Log (where trip_report_id is set)
- `mountains()` - HasManyThrough Mountain via Log

### Log Model (Updated)
Located at: `app/Models/Log.php`

**Updated Relationships:**
- `tripReport()` - BelongsTo TripReport (optional - only set when log is part of a trip report)
- `mountain()` - BelongsTo Mountain
- `user()` - BelongsTo User

## Controller

### TripReportController
Located at: `app/Http/Controllers/TripReportController.php`

**Methods:**

1. **store(Request $request)** - Create a new trip report
   - Validates: description, start_date, end_date, visibility, logs array
   - Creates trip report and associated logs
   - Automatically creates regular log entries for completed peaks (done=1)
   - Returns the created trip report with relationships loaded

2. **index()** - Get all trip reports for the authenticated user
   - Returns trip reports with logs and mountains loaded

3. **show($id)** - Get a specific trip report
   - Returns trip report with logs, mountains, and user loaded

4. **update(Request $request, $id)** - Update a trip report
   - Validates and updates trip report fields
   - If logs are provided, deletes old logs and creates new ones

5. **destroy($id)** - Delete a trip report
   - Soft delete or hard delete (depending on implementation)

6. **friends()** - Get trip reports from friends
   - Returns trip reports with visibility 'friends' or 'public'
   - NOTE: This needs to be integrated with your friendship system

7. **forMountain($mountainId)** - Get public trip reports for a specific mountain
   - Used for displaying trip reports on mountain detail pages
   - Returns public trip reports that include the specified mountain

## API Routes

### Protected Routes (auth:sanctum)

```
POST   /api/trip-reports                    - Create trip report
GET    /api/trip-reports                    - List user's trip reports
GET    /api/trip-reports/{id}               - Get specific trip report
PUT    /api/trip-reports/{id}               - Update trip report
DELETE /api/trip-reports/{id}               - Delete trip report
GET    /api/trip-reports/mountain/{id}      - Get trip reports for mountain (authenticated)
GET    /api/trip-reports/friends/index      - Get friends' trip reports
```

### Public Routes

```
GET    /api/trip-reports/mountain/{id}/public - Get public trip reports for mountain
```

## Request/Response Format

### Create Trip Report (POST /api/trip-reports)

**Request Body:**
```json
{
  "description": "Amazing hike in the Cairngorms",
  "start_date": "01/01/2025",
  "end_date": "05/01/2025",
  "visibility": "public",
  "logs": [
    {
      "mountain_id": 1,
      "done": 1,
      "date_climbed": "02/01/2025",
      "description": "Great conditions"
    },
    {
      "mountain_id": 2,
      "done": 1,
      "date_climbed": "03/01/2025",
      "description": ""
    }
  ]
}
```

**Response (201 Created):**
```json
{
  "message": "Trip report created successfully",
  "trip_report": {
    "id": 1,
    "user_id": 1,
    "description": "Amazing hike in the Cairngorms",
    "start_date": "01/01/2025",
    "end_date": "05/01/2025",
    "visibility": "public",
    "created_at": "...",
    "updated_at": "...",
    "logs": [
      {
        "id": 1,
        "trip_report_id": 1,
        "mountain_id": 1,
        "done": 1,
        "date_climbed": "02/01/2025",
        "description": "Great conditions",
        "mountain": {
          // Mountain details
        }
      }
    ]
  }
}
```

## Migration

Run the migrations:
```bash
php artisan migrate
```

This will create:
- `trip_reports` table
- Add `trip_report_id` column to existing `logs` table

## Integration with Log System

The trip report system uses the existing `logs` table. When a trip report is created:
- Log entries are created with `trip_report_id` set to the trip report's ID
- These logs appear both as part of the trip report AND in the user's regular log list
- Logs with `trip_report_id` set are part of a trip report
- Logs without `trip_report_id` are standalone logs

This unified approach means:
- No duplicate data storage
- Trip report logs are regular logs that are grouped by trip report
- Users see their completed peaks in both the trip report and their log list

## Visibility Levels

- **private**: Only visible to the creator
- **friends**: Visible to connected friends (requires friendship integration)
- **public**: Visible on mountain detail pages and to all users

## Next Steps

1. **Run Migrations:**
   ```bash
   php artisan migrate
   ```

2. **Test the API endpoints** using Postman or your frontend

3. **Integrate friendship logic** in the `friends()` method if needed

4. **Add authorization policies** if you want more granular access control

5. **Add validation for date formats** if needed (currently accepts strings)

6. **Consider adding indexes** on frequently queried columns:
   - `trip_reports.user_id`
   - `trip_reports.visibility`
- `logs.trip_report_id`
- `logs.mountain_id` (already exists)

## Notes

- Date formats are stored as strings in DD/MM/YYYY format as sent from the frontend
- The `friends()` method needs to be integrated with your friendship system
- Public trip reports can be accessed without authentication for mountain detail pages
- Trip reports automatically sync with the regular logs table for completed peaks

