<?php

namespace App\Components\HaClaim;

use App\Models\Claim\Claim;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\Lazy;
use Livewire\Component;

#[Lazy]
class View extends Component {

    public Claim $claim;
    public string $activeTab = 'overview';

    protected string $cacheKey;

    public function mount(Claim $claim): void {
        $this->claim = $claim;
        $this->cacheKey = $this->makeCacheKey();

        $this->activeTab = Cache::remember(
            $this->cacheKey,
            now()->addMonth(),
            fn() => 'overview'
        );
    }

    protected function makeCacheKey(): string {
        return sprintf(
            'ha_claim_tabs.user_%s.claim_%s',
            Auth::id() ?? 'guest',
            $this->claim->getKey()
        );
    }

    public function setTab(string $tab): void {
        $this->activeTab = $tab;

        Cache::put(
            $this->cacheKey ?? $this->makeCacheKey(),
            $tab,
            now()->addMonth()
        );
    }

    public function render() {
        return view('components.ha-claim.view', [
            'claim'  => $this->claim->load('client', 'status'),
            'client' => $this->claim->client,
        ]);
    }

    public function placeholder() {
        return <<<'HTML'
<div class="card p-4">
    <div class="placeholder-glow mb-3">
        <span class="placeholder col-4 placeholder-lg"></span>
    </div>
    <div class="placeholder-glow mb-2">
        <span class="placeholder col-8"></span>
    </div>
    <div class="placeholder-glow">
        <span class="placeholder col-6"></span>
    </div>
</div>
HTML;
    }
}
