<?php

namespace App\Components\Dialler;

use App\Models\Interaction;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class Manager extends Component {
    public $status;
    public $activeCall = null;

    public function mount() {
        $this->status = Auth::user()->agent_status ?? 'offline';
    }

    public function setStatus($newStatus) {
        $user = Auth::user();
        $user->agent_status = $newStatus;
        $user->save();

        $this->status = $newStatus;
    }

    public function endCall() {
        if ($this->activeCall) {
            $this->activeCall->update(['status' => 'completed']);
        }
    }

    public function getStatusButtonClass() {
        return match ($this->status) {
            'available' => 'btn-success',
            'busy' => 'btn-danger',
            'wrap_up' => 'btn-warning',
            'do_not_disturb' => 'btn-secondary',
            default => 'btn-secondary'
        };
    }

    public function getStatusIndicatorClass() {
        return match ($this->status) {
            'available' => 'border-success bg-primary',
            'busy' => 'border-danger bg-primary',
            'wrap_up' => 'border-warning bg-primary',
            'do_not_disturb' => 'border-danger bg-primary',
            default => 'border-secondary bg-primary'
        };
    }

    public function render() {
        $this->activeCall = Interaction::where('agent_id', Auth::id())
            ->whereIn('status', ['ringing', 'in-progress'])
            ->latest()
            ->first();
        return view('components.dialler.manager');
    }
}
