<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Call Model
 * 
 * Represents call records in the system
 * Used for tracking 3CX call history and logging
 */
class Call extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'calls';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'uniqueid',
        'user_id',
        'extension',
        'client_id',
        'client_type',
        'number',
        'call_type',
        'state',
        'dialed_time',
        'answered_time',
        'message',
        'tab_opened',
        'task_id'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'dialed_time' => 'datetime',
        'answered_time' => 'datetime',
        'tab_opened' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * Get the user who made the call
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the client (polymorphic relationship)
     */
    public function client()
    {
        return $this->morphTo('client', 'client_type', 'client_id');
    }

    /**
     * Get the task associated with the call
     */
    public function task(): BelongsTo
    {
        return $this->belongsTo(Task::class);
    }

    /**
     * Scope to get calls for a specific client
     */
    public function scopeForClient($query, $clientId, $clientType = null)
    {
        $query->where('client_id', $clientId);
        
        if ($clientType) {
            $query->where('client_type', $clientType);
        }
        
        return $query;
    }

    /**
     * Scope to get recent calls
     */
    public function scopeRecent($query, $days = 30)
    {
        return $query->where('created_at', '>=', now()->subDays($days));
    }

    /**
     * Get formatted duration
     */
    public function getDurationAttribute()
    {
        if (!$this->dialed_time || !$this->answered_time) {
            return null;
        }

        $seconds = $this->answered_time->diffInSeconds($this->dialed_time);
        
        if ($seconds < 60) {
            return $seconds . 's';
        }
        
        $minutes = floor($seconds / 60);
        $remainingSeconds = $seconds % 60;
        
        return $minutes . 'm ' . $remainingSeconds . 's';
    }

    /**
     * Get call status color for UI
     */
    public function getStatusColorAttribute()
    {
        return match($this->state) {
            'completed', 'answered' => 'success',
            'failed', 'rejected' => 'danger',
            'busy' => 'warning',
            'no_answer' => 'info',
            default => 'secondary'
        };
    }

    /**
     * Get call status icon for UI
     */
    public function getStatusIconAttribute()
    {
        return match($this->state) {
            'completed', 'answered' => 'fa-check-circle',
            'failed', 'rejected' => 'fa-times-circle',
            'busy' => 'fa-clock-o',
            'no_answer' => 'fa-phone-slash',
            'dialing' => 'fa-phone',
            default => 'fa-phone'
        };
    }
}
