<?php

namespace App\Observers;

use App\Models\ProductReview;
use Carbon\Carbon;

class ProductReviewObserver
{
    /**
     * Handle the ProductReview "created" event.
     */
    public function created(ProductReview $productReview): void
    {
        // Auto-flag suspicious reviews if enabled
        if (config('reviews.auto_flag_suspicious', true)) {
            $this->checkSuspiciousActivity($productReview);
        }

        // Profanity filter (placeholder - implement actual profanity detection if needed)
        if (config('reviews.enable_profanity_filter', false)) {
            $this->checkProfanity($productReview);
        }
    }

    /**
     * Check for suspicious activity (multiple reviews from same IP).
     */
    protected function checkSuspiciousActivity(ProductReview $review): void
    {
        if (!$review->ip_address) {
            return;
        }

        $threshold = config('reviews.suspicious_review_threshold.count', 3);
        $timeWindow = config('reviews.suspicious_review_threshold.minutes', 60);

        $recentReviews = ProductReview::where('ip_address', $review->ip_address)
            ->where('id', '!=', $review->id)
            ->where('created_at', '>=', Carbon::now()->subMinutes($timeWindow))
            ->count();

        if ($recentReviews >= ($threshold - 1)) {
            // Auto-flag for moderation
            $review->update(['is_approved' => false]);
        }
    }

    /**
     * Check for profanity in review content (placeholder).
     */
    protected function checkProfanity(ProductReview $review): void
    {
        // Placeholder for profanity detection
        // In production, you would integrate with a profanity detection service
        // or use a word list to check against
        
        $profanityWords = []; // Add profanity words list here or load from config
        
        $textToCheck = strtolower(($review->title ?? '') . ' ' . ($review->body ?? ''));
        
        foreach ($profanityWords as $word) {
            if (str_contains($textToCheck, strtolower($word))) {
                // Auto-flag for moderation
                $review->update(['is_approved' => false]);
                break;
            }
        }
    }

    /**
     * Handle the ProductReview "updated" event.
     */
    public function updated(ProductReview $productReview): void
    {
        // Re-check suspicious activity if content was updated
        if ($productReview->wasChanged(['title', 'body']) && config('reviews.auto_flag_suspicious', true)) {
            $this->checkSuspiciousActivity($productReview);
        }
    }

    /**
     * Handle the ProductReview "deleted" event.
     */
    public function deleted(ProductReview $productReview): void
    {
        //
    }

    /**
     * Handle the ProductReview "restored" event.
     */
    public function restored(ProductReview $productReview): void
    {
        //
    }

    /**
     * Handle the ProductReview "force deleted" event.
     */
    public function forceDeleted(ProductReview $productReview): void
    {
        //
    }
}
