<?php

namespace App\Livewire\Projects\Traits;

use App\Models\Phase;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\On;

trait MapLayers
{
    public $phases;

    public $activeLayers = [];  // Now stores Layer IDs (not Phase IDs)

    public $showLayerModal = false;

    // ─── Layer / Phase visibility ────────────────────────────────────────────────

    public function toggleLayer($layerId)
    {
        $layerId = (int) $layerId;
        if (in_array($layerId, $this->activeLayers)) {
            $this->activeLayers = array_values(array_diff($this->activeLayers, [$layerId]));
        } else {
            $this->activeLayers[] = $layerId;
        }
        $this->dispatch('layersUpdated', $this->activeLayers);
    }

    public function togglePhase($phaseId)
    {
        $phase = $this->phases->find($phaseId);
        if (! $phase) {
            return;
        }
        $layerIds = $phase->layers->pluck('id')->map(fn ($id) => (int) $id)->toArray();
        $allActive = ! empty($layerIds) && collect($layerIds)->every(fn ($id) => in_array($id, $this->activeLayers));
        if ($allActive) {
            $this->activeLayers = array_values(array_diff($this->activeLayers, $layerIds));
        } else {
            $this->activeLayers = array_values(array_unique(array_merge($this->activeLayers, $layerIds)));
        }
        $this->dispatch('layersUpdated', $this->activeLayers);
    }

    public function openLayerModal()
    {
        $this->showLayerModal = true;
    }

    public function closeLayerModal()
    {
        $this->showLayerModal = false;
    }

    // ─── Filters ────────────────────────────────────────────────────────────────

    public $filterStatus = '';

    public $filterResponsible = '';

    public $filterProgressMin = 0;

    public $filterProgressMax = 100;

    public $showFilters = false;

    public function updatedFilterStatus()
    {
        $this->applyFilters();
    }

    public function updatedFilterResponsible()
    {
        $this->applyFilters();
    }

    public function updatedFilterProgressMin()
    {
        $this->applyFilters();
    }

    public function updatedFilterProgressMax()
    {
        $this->applyFilters();
    }

    public function applyFilters()
    {
        $filtered = $this->allFeatures->filter(function ($f) {
            if ($this->filterStatus && $f->status !== $this->filterStatus) {
                return false;
            }
            if ($this->filterResponsible && ! str_contains(strtolower($f->responsible ?? ''), strtolower($this->filterResponsible))) {
                return false;
            }
            if ($f->progress < $this->filterProgressMin || $f->progress > $this->filterProgressMax) {
                return false;
            }

            return true;
        });
        $this->dispatch('filtersChanged', $filtered->pluck('id')->values()->toArray());
    }

    public function clearFilters()
    {
        $this->filterStatus = '';
        $this->filterResponsible = '';
        $this->filterProgressMin = 0;
        $this->filterProgressMax = 100;
        $this->dispatch('filtersChanged', $this->allFeatures->pluck('id')->values()->toArray());
    }
}