<?php

namespace App\Models;

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

class OperationTask extends Model
{
    use HasFactory;

    protected $fillable = [
        'client_id',
        'title',
        'description',
        'category',
        'priority',
        'status',
        'source_type',
        'source_id',
        'assigned_to',
        'due_at',
        'completed_at',
        'meta',
        'created_by',
    ];

    protected $casts = [
        'due_at' => 'datetime',
        'completed_at' => 'datetime',
        'meta' => 'array',
    ];

    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    public function assignee(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_to');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function scopeOpen($query)
    {
        return $query->whereIn('status', ['open', 'in_progress', 'waiting']);
    }

    public function scopeOverdue($query)
    {
        return $query->open()->whereNotNull('due_at')->where('due_at', '<', now()->startOfDay());
    }

    public function scopeDueToday($query)
    {
        return $query->open()->whereDate('due_at', today());
    }

    public function priorityLabel(): string
    {
        return match ($this->priority) {
            'critical' => 'Kritik',
            'high' => 'Yüksek',
            'medium' => 'Orta',
            'low' => 'Düşük',
            default => ucfirst((string) $this->priority),
        };
    }

    public function priorityBadgeClass(): string
    {
        return match ($this->priority) {
            'critical' => 'badge-danger',
            'high' => 'badge-warning',
            'medium' => 'badge-info',
            'low' => 'badge-secondary',
            default => 'badge-light',
        };
    }

    public function statusLabel(): string
    {
        return match ($this->status) {
            'open' => 'Açık',
            'in_progress' => 'İşlemde',
            'waiting' => 'Bekliyor',
            'completed' => 'Tamamlandı',
            'cancelled' => 'İptal',
            default => ucfirst((string) $this->status),
        };
    }

    public function statusBadgeClass(): string
    {
        return match ($this->status) {
            'open' => 'badge-primary',
            'in_progress' => 'badge-info',
            'waiting' => 'badge-warning',
            'completed' => 'badge-success',
            'cancelled' => 'badge-secondary',
            default => 'badge-light',
        };
    }

    public function isOverdue(): bool
    {
        return $this->due_at !== null
            && ! in_array($this->status, ['completed', 'cancelled'], true)
            && $this->due_at->lt(now()->startOfDay());
    }

    public function dueLabel(): string
    {
        if (! $this->due_at) {
            return 'Tarih yok';
        }

        if ($this->isOverdue()) {
            return 'Gecikti: '.$this->due_at->format('d.m.Y H:i');
        }

        if ($this->due_at->isToday()) {
            return 'Bugün '.$this->due_at->format('H:i');
        }

        return $this->due_at->format('d.m.Y H:i');
    }
}
