<?php

namespace App\Livewire\LicenseTokens\Components;

use App\Models\Clients\Client;
use App\Models\Environments\Environment;
use App\Models\Products\Product;
use Livewire\Component;

class Review extends Component
{
    public ?int $clientId = null;
    public ?string $token = null;
    public ?string $name = null;
    public ?string $startAt = null;
    public ?string $endAt = null;
    public ?string $observation = null;
    public string $environmentMode = 'existing';
    public array $selectedEnvironmentIds = [];
    public array $newEnvironmentData = [];
    public array $selectedProductIds = [];

    public function mount(
        ?int $clientId = null,
        ?string $token = null,
        ?string $name = null,
        ?string $startAt = null,
        ?string $endAt = null,
        ?string $observation = null,
        string $environmentMode = 'existing',
        array $selectedEnvironmentIds = [],
        array $newEnvironmentData = [],
        array $selectedProductIds = []
    ) {
        $this->clientId = $clientId;
        $this->token = $token;
        $this->name = $name;
        $this->startAt = $startAt;
        $this->endAt = $endAt;
        $this->observation = $observation;
        $this->environmentMode = $environmentMode;
        $this->selectedEnvironmentIds = $selectedEnvironmentIds;
        $this->newEnvironmentData = $newEnvironmentData;
        $this->selectedProductIds = $selectedProductIds;
    }

    public function render()
    {
        $client = $this->clientId ? Client::find($this->clientId) : null;
        $environments = !empty($this->selectedEnvironmentIds) 
            ? Environment::with('type')->whereIn('id', $this->selectedEnvironmentIds)->get() 
            : collect();
        $products = !empty($this->selectedProductIds) 
            ? Product::whereIn('id', $this->selectedProductIds)->get() 
            : collect();

        return view('livewire.license-tokens.components.review', [
            'client' => $client,
            'environments' => $environments,
            'products' => $products,
        ]);
    }
}

