<?php

namespace App\Livewire\Forms\Modals;

use App\Models\Room;
use App\Models\Branch;

use Livewire\Component;
use App\Services\ApiClient;
use Illuminate\Support\Facades\Auth;
use WireUi\Traits\WireUiActions;

class RoomDetailModal extends Component
{
    use WireUiActions;

    protected $listeners = [
        'openModal', 'closeModal', 
        'startNotify', 'stopNotify', 'fireNotify', 
        'openAccountNotify','closeAccountNotify'
    ];

    public $room_name;
    public $branch;
    public bool $showModal = false;

    public function openModal($roomId)
    {
        $room = Room::find($roomId);
        $this->room_name=$room->type->value . $room->name;
        $this->branch = Branch::find($room->branch_id);
        $this->showModal = true;
    }
    public function closeModal()
    {
        $this->showModal = false;
    }
    public function startNotify()
    {
        $data = $this->buildNotifyData('active', now(), null);
        $this->send($data);
    }

    public function stopNotify()
    {
        $data = $this->buildNotifyData('closed', null, null);
        $this->send($data);
    }

    public function fireNotify()
    {
        $data = $this->buildNotifyData('fire', null, null);
        $this->send($data);
    }

    public function openAccountNotify()
    {
        $data = $this->buildNotifyData('active', now(), null);
        $this->send($data);
    }

    public function closeAccountNotify()
    {
        $data = $this->buildNotifyData('closed', now(), null);
        $this->send($data);
    }
    protected function buildNotifyData(string $command, $startedAt = null, $endedAt = null): array
    {
        return [
            'branch_name'  => $this->branch->name ?? '',  
            'room_name'  => $this->room_name ?? '',
            'command'    => $command,
            'started_at' => $startedAt ? $startedAt->toDateTimeString() : null,
            'ended_at'   => $endedAt ? $endedAt->toDateTimeString() : null,
        ];
    }

    function send(array $data){
        $user = Auth::user();
        $token = $user->api_plain_token ?? null;

        if (!$token) {
            $this->sendErrorNotification('api', 'API token is missing.');
            return false;
        }

        $api = new ApiClient(config('app.url') , $token );
        $response = $api->post('/api/room/sendSwitch', $data);
        if ($response->failed()) {
            $this->sendErrorNotification('api', 'API request failed: ' . $response->body());
            return false;
        }
        // ✅ 成功提示
        $this->notification()->send([
            'icon' => 'success',
            'title' => '成功',
            'description' => '命令已成功發送：' . $data['command'],
        ]);
        // ✅ 關閉 modal
        $this->showModal = false;
        return true;
    }
    public function sendErrorNotification(string $title = '錯誤', string $description = '發生未知錯誤')
    {
        $this->notification()->send([
            'icon' => 'error',
            'title' => $title,
            'description' =>$description,
        ]);
    }
    public function render()
    {
        return view('livewire.forms.modals.room-detail-modal');
    }
}
