<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Task extends Model
{
    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'user_id',
        'title',
        'description',
        'status',
        'priority',
        'due_at',
        'completed_at',
        'reminder_at',
        'reminder_sent',
    ];

    protected $casts = [
        'due_at'        => 'datetime',
        'completed_at'  => 'datetime',
        'reminder_at'   => 'datetime',
        'reminder_sent' => 'boolean',
    ];

    const STATUS_PENDING     = 'pending';
    const STATUS_IN_PROGRESS = 'in_progress';
    const STATUS_DONE        = 'done';

    const PRIORITY_LOW    = 'low';
    const PRIORITY_MEDIUM = 'medium';
    const PRIORITY_HIGH   = 'high';

    protected static function boot(): void
    {
        parent::boot();

        static::creating(function ($model) {
            if (!$model->id) {
                $model->id = (string) Str::uuid();
            }
        });
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function scopeForUser(Builder $query, string $userId): Builder
    {
        return $query->where('user_id', $userId);
    }

    public function scopeSearch(Builder $query, string $term): Builder
    {
        return $query->where(function ($q) use ($term) {
            $q->where('title', 'like', "%{$term}%")
              ->orWhere('description', 'like', "%{$term}%");
        });
    }

    public function scopeOfStatus(Builder $query, string $status): Builder
    {
        if ($status === 'today') {
            return $query->whereDate('due_at', today())
                         ->where('status', '!=', self::STATUS_DONE);
        }

        return $query->where('status', $status);
    }

    public function isDone(): bool
    {
        return $this->status === self::STATUS_DONE;
    }

    public function isOverdue(): bool
    {
        return $this->due_at
            && $this->due_at->isPast()
            && !$this->isDone();
    }

    /** Tailwind classes für Priorität */
    public function priorityClasses(): array
    {
        return match ($this->priority) {
            'high'  => ['dot' => 'bg-red-500',    'badge' => 'bg-red-50 text-red-700'],
            'low'   => ['dot' => 'bg-gray-300',   'badge' => 'bg-gray-50 text-gray-500'],
            default => ['dot' => 'bg-amber-400',  'badge' => 'bg-amber-50 text-amber-700'],
        };
    }

    public function priorityLabel(): string
    {
        return match ($this->priority) {
            'high'  => 'Hoch',
            'low'   => 'Niedrig',
            default => 'Mittel',
        };
    }

    public function statusLabel(): string
    {
        return match ($this->status) {
            'in_progress' => 'In Bearbeitung',
            'done'        => 'Erledigt',
            default       => 'Offen',
        };
    }
}
