<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

final class Review extends Model
{
    /** @use HasFactory<\Database\Factories\ReviewFactory> */
    use HasFactory, LogsActivity;

    protected $fillable = [
        'property_id',
        'guest_id',
        'booking_id',
        'rating',
        'cleanliness_rating',
        'accuracy_rating',
        'communication_rating',
        'location_rating',
        'value_rating',
        'title',
        'comment',
        'owner_response',
        'owner_response_date',
        'status',
        'is_featured',
    ];

    protected function casts(): array
    {
        return [
            'property_id' => 'integer',
            'guest_id' => 'integer',
            'booking_id' => 'integer',
            'rating' => 'decimal:1',
            'cleanliness_rating' => 'decimal:1',
            'accuracy_rating' => 'decimal:1',
            'communication_rating' => 'decimal:1',
            'location_rating' => 'decimal:1',
            'value_rating' => 'decimal:1',
            'is_featured' => 'boolean',
            'owner_response_date' => 'datetime',
            'created_at' => 'datetime',
            'updated_at' => 'datetime',
        ];
    }

    // Relationships

    public function property(): BelongsTo
    {
        return $this->belongsTo(Property::class);
    }

    public function guest(): BelongsTo
    {
        return $this->belongsTo(User::class, 'guest_id');
    }

    public function booking(): BelongsTo
    {
        return $this->belongsTo(Booking::class);
    }

    // Scopes

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeFeatured($query)
    {
        return $query->where('is_featured', true);
    }

    public function scopeForProperty($query, int $propertyId)
    {
        return $query->where('property_id', $propertyId);
    }

    public function scopeForGuest($query, int $guestId)
    {
        return $query->where('guest_id', $guestId);
    }

    public function scopeWithResponse($query)
    {
        return $query->whereNotNull('owner_response');
    }

    public function scopeWithoutResponse($query)
    {
        return $query->whereNull('owner_response');
    }

    // Helper Methods

    public function hasOwnerResponse(): bool
    {
        return ! is_null($this->owner_response);
    }

    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }

    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isRejected(): bool
    {
        return $this->status === 'rejected';
    }

    /**
     * Get average of all category ratings
     */
    public function categoryAverage(): float
    {
        $ratings = array_filter([
            $this->cleanliness_rating,
            $this->accuracy_rating,
            $this->communication_rating,
            $this->location_rating,
            $this->value_rating,
        ]);

        if (empty($ratings)) {
            return (float) $this->rating;
        }

        return round(array_sum($ratings) / count($ratings), 1);
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['status', 'rating'])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}
