<?php

namespace App\Livewire\Environments;

use App\Models\Environments\Environment;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
    use WithPagination;

    public string $search = '';

    public ?string $envFilter = null;

    public bool $onlyCoreMajorUpdates = false;

    public bool $onlyCoreMinorUpdates = false;

    public bool $onlyEnvironmentsWithOutdatedPlugins = false;

    public ?string $pluginComponentFilter = null;

    public ?int $productFilter = null;

    public function updatedSearch(): void
    {
        $this->resetPage();
    }

    public function updatedEnvFilter(): void
    {
        $this->resetPage();
    }

    public function updatedOnlyCoreMajorUpdates(): void
    {
        $this->resetPage();
    }

    public function updatedOnlyCoreMinorUpdates(): void
    {
        $this->resetPage();
    }

    public function updatedOnlyEnvironmentsWithOutdatedPlugins(): void
    {
        $this->resetPage();
    }

    public function updatedPluginComponentFilter(): void
    {
        $this->resetPage();
    }

    public function updatedProductFilter(): void
    {
        $this->resetPage();
    }

    public function render()
    {
        $environments = Environment::with(['client', 'type', 'licenseToken'])
            ->withCount([
                'plugins as outdated_plugins_count' => function ($pluginQuery) {
                    $pluginQuery->where('has_updates', true);
                },
            ])
            ->when($this->search !== '', function ($query) {
                $term = '%' . trim($this->search) . '%';
                $query->where(function ($q) use ($term) {
                    $q->where('name', 'like', $term)
                        ->orWhere('domain', 'like', $term)
                        ->orWhereHas('client', function ($subQuery) use ($term) {
                            $subQuery->where('name', 'like', $term);
                        });
                });
            })
            ->when($this->envFilter !== null && $this->envFilter !== '', function ($query) {
                $query->where('env', $this->envFilter);
            })
            ->when($this->onlyCoreMajorUpdates, function ($query) {
                $query->whereNotNull('lastversion');
            })
            ->when($this->onlyCoreMinorUpdates, function ($query) {
                $query->whereNotNull('lastminor');
            })
            ->when($this->onlyEnvironmentsWithOutdatedPlugins, function ($query) {
                $query->whereHas('plugins', function ($pluginQuery) {
                    $pluginQuery->where('has_updates', true);
                });
            })
            ->when($this->pluginComponentFilter !== null && $this->pluginComponentFilter !== '', function ($query) {
                $term = '%' . trim($this->pluginComponentFilter) . '%';
                $query->whereHas('plugins', function ($pluginQuery) use ($term) {
                    $pluginQuery->where('component', 'like', $term);
                });
            })
            ->when($this->productFilter !== null, function ($query) {
                $query->whereHas('licenseToken.products', function ($productQuery) {
                    $productQuery->where('products.id', $this->productFilter);
                });
            })
            ->orderBy('name')
            ->paginate(20);

        return view('livewire.environments.index', [
            'environments' => $environments,
        ])->layout('layouts.app');
    }
}

