<?php


namespace App\Livewire\Agent\Modals;

use App\Models\Activity;
use App\Services\AgentActionService;
use LivewireUI\Modal\ModalComponent;

class ConflictModal extends ModalComponent
{
    public $data = [];
    public $meta = [];
    public $originalInput = '';

    public $selectedSlot = null; // 'prev' | 'next' | null

    public function mount($data, $meta, $originalInput)
    {
        $this->data = $data;
        $this->meta = $meta;
        $this->originalInput = $originalInput;
    }

    public function selectSlot($type)
    {
        $this->selectedSlot = $this->selectedSlot === $type ? null : $type;
    }

    public function save()
    {
        // 👉 SLOT ÜBERNEHMEN
        if ($this->selectedSlot === 'prev' && !empty($this->meta['suggestion_prev'])) {
            $this->data['datetime'] = $this->meta['suggestion_prev'];
            $this->data['duration_minutes'] = $this->meta['duration'];
        } elseif ($this->selectedSlot === 'next' && !empty($this->meta['suggestion_next'])) {
            $this->data['datetime'] = $this->meta['suggestion_next'];
            $this->data['duration_minutes'] = $this->meta['duration'];
        }

        // 👉 IMMER force = true (weil Konflikt bewusst)
        $this->data['force'] = true;

        $result = AgentActionService::handle(auth()->user(), [
            'type' => 'event',
            'data' => $this->data,
        ]);

        if ($result['status'] === 'success') {
            $this->dispatch('eventCreated');

            Activity::log(
                auth()->id(),
                Activity::TYPE_EVENT_CREATED,
                Activity::localizedTitle('event_created_assistant', auth()->user()->locale ?? 'de'),
                $result['meta']['title'] ?? null,
                meta: ['start' => $result['meta']['start'] ?? null]
            );
        }

        $this->dispatch('agent:result', $result);
        $this->dispatch('closeModal');
    }

    public function render()
    {
        return view('livewire.agent.modals.conflict-modal');
    }
}

//
//namespace App\Livewire\Agent\Modals;
//
//use App\Services\AgentActionService;
//use LivewireUI\Modal\ModalComponent;
//
//class ConflictModal extends ModalComponent
//{
//    public $data = [];
//    public $meta = [];
//    public $originalInput = '';
//    public $selectedSlot = null; // 'prev' | 'next' | null
//
//    public function mount($data, $meta, $originalInput)
//    {
//        $this->data = $data;
//        $this->meta = $meta;
//        $this->originalInput = $originalInput;
//    }
//
//    public function selectSlot($type)
//    {
//        if ($this->selectedSlot === $type) {
//            // toggle off
//            $this->selectedSlot = null;
//        } else {
//            $this->selectedSlot = $type;
//        }
//    }
//
//    public function forceSave()
//    {
//        $this->data['force'] = true;
//
//        $result = AgentActionService::handle(auth()->user(), [
//            'type' => 'event',
//            'data' => $this->data
//        ]);
//
//        $this->dispatch('agent:result', $result);
//        $this->dispatch('closeModal');
//    }
//
//    public function acceptSuggestion($type)
//    {
//        if ($type === 'next' && !empty($this->meta['suggestion_next'])) {
//            $this->data['start'] = $this->meta['suggestion_next'];
//        }
//
//        if ($type === 'prev' && !empty($this->meta['suggestion_prev'])) {
//            $this->data['start'] = $this->meta['suggestion_prev'];
//        }
//
//        $this->data['force'] = true;
//
//        $result = AgentActionService::handle(auth()->user(), [
//            'type' => 'event',
//            'data' => $this->data
//        ]);
//
//        $this->dispatch('agent:result', $result);
//        $this->dispatch('closeModal');
//    }
//
//    public function render()
//    {
//        return view('livewire.agent.modals.conflict-modal');
//    }
//}
