# Review System Models

## Review Model
Represents user reviews for rides in the system.

### Properties
- `id` (int) - Primary key
- `ride_id` (int) - Foreign key to rides table
- `user_id` (int) - Foreign key to users table
- `rating` (int) - Rating from 1 to 5
- `title` (string, nullable) - Optional review title
- `content` (text) - Review content
- `status` (enum) - ReviewStatus enum value
- `moderated_at` (timestamp) - When review was moderated
- `moderated_by` (int) - Foreign key to users table (moderator)
- `helpful_votes_count` (int) - Counter cache for helpful votes

### Relationships
- `ride` - BelongsTo relationship to Ride model
- `user` - BelongsTo relationship to User model
- `moderator` - BelongsTo relationship to User model
- `helpfulVotes` - HasMany relationship to HelpfulVote model

### Scopes
- `pending()` - Reviews awaiting moderation
- `approved()` - Approved reviews
- `rejected()` - Rejected reviews
- `byRide()` - Filter by ride
- `byUser()` - Filter by user

### Methods
- `approve()` - Approve the review
- `reject()` - Reject the review
- `moderate()` - General moderation method
- `toggleHelpfulVote()` - Toggle helpful vote from a user

## HelpfulVote Model
Represents users marking reviews as helpful.

### Properties
- `id` (int) - Primary key
- `review_id` (int) - Foreign key to reviews table
- `user_id` (int) - Foreign key to users table
- `created_at` (timestamp) - When vote was cast

### Relationships
- `review` - BelongsTo relationship to Review model
- `user` - BelongsTo relationship to User model

### Methods
- `toggle()` - Toggle vote status

## Database Considerations

### Indexes
1. Reviews Table:
   - Primary Key: id
   - Foreign Keys: ride_id, user_id, moderated_by
   - Composite: [ride_id, user_id] (unique)
   - Indexes: [ride_id, status], [user_id, created_at], status

2. Helpful Votes Table:
   - Primary Key: id
   - Foreign Keys: review_id, user_id
   - Composite: [review_id, user_id] (unique)
   - Indexes: [user_id, created_at]

### Constraints
1. Reviews:
   - One review per ride per user
   - Rating must be between 1 and 5
   - Status must be valid enum value
   - Content is required

2. Helpful Votes:
   - One vote per review per user
   - Cascading deletes with review

## Usage Examples

```php
// Create a review
$review = Review::create([
    'ride_id' => $ride->id,
    'user_id' => Auth::id(),
    'rating' => 5,
    'title' => 'Great ride!',
    'content' => 'This was amazing...',
]);

// Toggle helpful vote
$review->toggleHelpfulVote(Auth::id());

// Moderate a review
$review->moderate(ReviewStatus::APPROVED, Auth::id());

// Get ride's approved reviews
$reviews = $ride->reviews()->approved()->latest()->get();

// Get user's helpful votes
$votes = $user->helpfulVotes()->with('review')->get();
```

## Implementation Notes

### Review Model Implementation

The Review model has been implemented in `app/Models/Review.php` with the following features:

- Fillable properties for all required fields
- Proper type casting for enum values and timestamps
- All relationships defined as specified (ride, user, moderator, helpfulVotes)
- Query scopes for filtering by status and relationships
- Methods for moderation and helpful vote management

### HelpfulVote Model Implementation

The HelpfulVote model has been implemented in `app/Models/HelpfulVote.php` with:

- Minimal properties (review_id, user_id)
- Relationships to Review and User models
- Static toggle method for easy vote management

### Ride Model Enhancements

The Ride model has been enhanced with review-related functionality:

- Added reviews() and approvedReviews() relationships
- Implemented getAverageRatingAttribute() for easy access to average ratings
- Added getReviewCountAttribute() for counting approved reviews
- Created canBeReviewedBy() method to check if a user can review a ride
- Implemented addReview() method for creating new reviews

These implementations maintain feature parity with the Django implementation while leveraging Laravel's Eloquent ORM features.