<?php

namespace App\Components\Claim\Application;

use App\Models\Client\Client;
use App\Services\ApplicationService;
use App\Services\EsignService;
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'],
        'provisional-value' => ['label' => '10 - Provisional Value', 'icon' => 'bi-bank'],
    ];

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

    public function generateApplication(ApplicationService $service) {
        try {
            $service->build($this->client);
            $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) {
        $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->dispatch('toast', message: "Request sent.", type: 'success');
        } else {
            $this->dispatch('toast', message: "Request not sent.", type: 'error');
        }
    }

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