<?php

namespace App\Components\Claim\Application;

use App\Models\Client\Client;
use App\Services\ApplicationService;
use App\Services\EsignService;
use App\Services\HistoryService;
use Illuminate\Support\Facades\Log;
use Livewire\Component;

class Application extends Component {
    public Client $client;
    public $activeTab = 'care-settings';

    public $tabs = [
        'personal-details' => ['label' => 'Section 1 - Personal Details', 'icon' => 'bi-heart-pulse'],
        'contact-preferences' => ['label' => 'Section 2 - Contacting Client', 'icon' => 'bi-heart-pulse'],
        'confirm-identity' => ['label' => 'Section 3 - Confirming Client Identity', 'icon' => 'bi-heart-pulse'],
        'terminal-illness' => ['label' => 'Section 4 - Terminal Illness', 'icon' => 'bi-heart-pulse'],
        'beneficiary' => ['label' => 'Section 5 - Nominated Beneficiary', 'icon' => 'bi-people'],
        'redress-type' => ['label' => 'Section 6 - Redress Type', 'icon' => 'bi-people'],
        'care-settings' => ['label' => 'Section 7 - Care Settings', 'icon' => 'bi-hospital'],
        'further-information' => ['label' => 'Section 7a - Further Information', 'icon' => 'bi-hospital'],
        'impact-factors' => ['label' => 'Section 8 - Impact Factors', 'icon' => 'bi-exclamation-triangle'],
        'bank-details' => ['label' => 'Section 9 - Bank Details', 'icon' => 'bi-bank'],
    ];

    /**
     * update the active tab
     */
    public function setTab($tab)
    {
        $this->activeTab = $tab;
    }

    public function generateApplication(ApplicationService $service, HistoryService $history) {
        try {
            $service->build($this->client);

            $this->recordHistory($history, 'Application document generated');

            $this->dispatch('toast', message: "Application generated and saved.", type: 'success');
        } catch (\Exception $exception) {
            $this->dispatch('toast', message: $exception->getMessage(), type: 'error');
            Log::error($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
        }
    }

    public function sendCFARequest(EsignService $esign, HistoryService $history) {
        $docType = ['CFA', 'LOA'];
        if($esign->send(
            email: (string)$this->client->email_address,
            phone: $this->client->phone_number,
            documentType: $docType,
            prefillData: [],
            externalId: $this->client->slug,
        )) {
            $this->recordHistory($history, 'CFA/LOA e-sign request sent to client', [
                'documents' => $docType,
                'email'     => (string) $this->client->email_address,
            ]);
            $this->dispatch('toast', message: "Request sent.", type: 'success');
        } else {
            $this->recordHistory($history, 'CFA/LOA e-sign request failed to send', [
                'documents' => $docType,
            ]);
            $this->dispatch('toast', message: "Request not sent.", type: 'error');
        }
    }

    private function recordHistory(HistoryService $history, string $event, array $context = []): void
    {
        if ($claim = $this->client->claim) {
            $history->addForClaim($claim, $event, $context);
        } else {
            $history->add($this->client, $event, $context);
        }
    }

    public function render()
    {
        return view('components.claim.application.application');
    }
}