<?php

namespace App\Listeners;

use App\Models\Bank;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Support\Facades\Log;

class ScoutReindexListener
{
    /**
     * Handle tag-related events
     */
    public function handleTagEvent($event)
    {
        try {
            Log::info('Scout: Tag event triggered, reindexing searchable models');

            // Reindex all searchable models when tags change
            $this->reindexAllSearchableModels();

        } catch (\Exception $e) {
            Log::error('Scout reindex failed for tag event: ' . $e->getMessage());
        }
    }

    /**
     * Handle pivot events (tag/location attachments)
     */
    public function handlePivotEvent($event)
    {
        try {
            $model = $event->model ?? null;

            if ($model && method_exists($model, 'searchable')) {
                Log::info('Scout: Pivot event for ' . get_class($model) . ' ID: ' . $model->id);
                $model->searchable();
            }

        } catch (\Exception $e) {
            Log::error('Scout reindex failed for pivot event: ' . $e->getMessage());
        }
    }

    /**
     * Handle location-related events
     */
    public function handleLocationEvent($event)
    {
        try {
            Log::info('Scout: Location event triggered, reindexing searchable models');

            // Reindex all searchable models when locations change
            $this->reindexAllSearchableModels();

        } catch (\Exception $e) {
            Log::error('Scout reindex failed for location event: ' . $e->getMessage());
        }
    }

    /**
     * Reindex a specific model instance
     */
    public function reindexModel($model)
    {
        try {
            if ($model && method_exists($model, 'searchable')) {
                Log::info('Scout: Manual reindex for ' . get_class($model) . ' ID: ' . $model->id);
                $model->searchable();
            }
        } catch (\Exception $e) {
            Log::error('Scout manual reindex failed: ' . $e->getMessage());
        }
    }

    /**
     * Reindex all searchable models
     */
    protected function reindexAllSearchableModels()
    {
        $searchableModels = [
            User::class,
            Organization::class,
            Bank::class,
        ];

        foreach ($searchableModels as $modelClass) {
            try {
                if (class_exists($modelClass) && method_exists($modelClass, 'makeAllSearchable')) {
                    Log::info("Scout: Reindexing all {$modelClass} records");

                    // Use chunking for large datasets
                    $modelClass::chunk(100, function ($models) {
                        foreach ($models as $model) {
                            if (method_exists($model, 'searchable')) {
                                $model->searchable();
                            }
                        }
                    });
                }
            } catch (\Exception $e) {
                Log::error("Scout reindex failed for {$modelClass}: " . $e->getMessage());
            }
        }
    }
}