<?php

namespace App\Livewire\Buttons;

use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Livewire\WithFileUploads;

class UpdateCardType extends Component
{
    use WithFileUploads;

    //card type details
    public $uuid;
    public $card_code;
    public $card_name;
    public $card_description;
    public $card_image;
    public $card_cover_image;
    public $card_font_color = 0;
    public $id_number = 0;
    public $id_description;

    //image preview handler
    public $type_image;
    public $cover_image;

    //data privacy
    public $terms_condition;
    public $faqs;

    public function mount($uuid)
    {
        $this->uuid = $uuid;
        $token = Session::get('user')['access_token'] ?? null;

        if (!$token) {
            $this->addError('auth', 'No access token found.');
            return;
        }

        $response = Http::withToken($token)->get(config('services.backend_api.url') . "/api/cms/cardType/{$uuid}");
        if ($response->successful()) {
            $data = $response->json('data');

            $this->card_code = $data['code'] ?? '';
            $this->card_name = $data['name'] ?? '';
            $this->card_description = $data['description'] ?? '';
            $this->card_font_color = $data['virtual_card_font_color'] ?? '';
            $this->id_number = $data['id_number'] ?? 0;
            $this->id_description = $data['id_number_description'] ?? '';
            $this->terms_condition = $data['terms_and_conditions'] ?? '';
            $this->faqs = $data['faqs'] ?? '';
            $this->type_image = $data['image'] ?? '';
            $this->cover_image = $data['bg_image'] ?? '';
        } else {
            $this->addError('card types', 'Failed to fetch card type.');
        }
    }


    public function submit()
    {

        $token = Session::get('user')['access_token'] ?? null;


        if (!$token) {
            $this->addError('users', 'No access token found.');
            return;
        }

        if (!$this->card_image) {
            $this->addError('image', 'Please upload an image.');
            return;
        }

        if (!$this->card_cover_image) {
            $this->addError('image', 'Please upload an image.');
            return;
        }

        $response = Http::withToken($token)
            ->attach(
                'image',                             //card type image
                file_get_contents($this->card_image->getRealPath()),
                $this->card_image->getClientOriginalName()
            )
            ->attach(
                'bg_image',                            //card type cover image
                file_get_contents($this->card_cover_image->getRealPath()),
                $this->card_cover_image->getClientOriginalName()
            )
            ->post(config('services.backend_api.url') . '/api/cms/cardTypeUpdate/' . $this->uuid, [
                'code'                          => $this->card_code,
                'type'                          => $this->card_name,
                'description'                   => $this->card_description,
                'virtual_card_font_color'       => $this->card_font_color,
                'id_number'                     => $this->id_number,
                'id_number_description'         => $this->id_description,
                'terms_and_conditions'          => $this->terms_condition,
                'faqs'                          => $this->faqs,
            ]);

        // dd($response->json());

        //handle backend response
        if ($response->status() === 422) {
            $errors = $response->json('data');
            foreach ($errors as $field => $messages) {
                $this->addError($field, $messages[0]);
            }
            return;
        }

        if ($response->successful()) {
            session()->flash('success', 'Card type updated successfully.');
            return redirect('/main/about-us/card-type');
        } else {
            $this->addError('card types', 'Failed to update card type.');
        }
    }

    public function cancel()
    {
        return redirect()->to('/main/about-us/card-type');
    }
    public function render()
    {
        return view('livewire.buttons.update-card-type');
    }
}
