<?php

namespace App\Livewire\Buttons;

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

class CreatePhotoSlider extends Component
{
    use WithFileUploads;
    public $title;
    public $description;
    public $start_date;
    public $end_date;
    public $start_time;
    public $end_time;
    public $image;


    //get promotions
    public $promotions = [];
    public $promotion_id;
    // public $selectedPromotion = '';

    public function mount()
    {
        $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/promotion');

        if ($response->successful()) {
            $this->promotions = $response->json('data'); // Adjust if response shape is different
        } else {
            $this->addError('promotions', 'Failed to load promotions.');
        }
    }

    public function submit()
    {

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


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

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

//         dd([
//     'start_date' => $this->start_date,
//     'end_date' => $this->end_date,
// ]);

        $response = Http::withToken($token)
            ->attach(
                'image',                      // field name expected by backend
                file_get_contents($this->image->getRealPath()),  // file content
                $this->image->getClientOriginalName()            // original filename
            )
            ->post(config('services.backend_api.url') . '/api/cms/photoSlider', [
                'promotion_uuid' => $this->promotion_id,
                'title'   => $this->title,
                'description' => $this->description,
                'date_start'  => $this->start_date,
                'date_end'   => $this->end_date,
            ]);

            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', 'Photo slider created successfully.');
            return redirect('/main/home-page-mobile/photo-slider');
        } else {
            $this->addError('Photo slider', 'Failed to create photo slider.');
        }
    }

    public function cancel()
    {
        return redirect()->to('/main/home-page-mobile/photo-slider');
    }

    public function render()
    {
        return view('livewire..buttons.create-photo-slider');
    }
}
