<?php

namespace App\Models;

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

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

    protected $fillable = [
        'user_id',
        'title',
        'content',
        'color',
        'pinned',
    ];

    protected $casts = [
        'pinned' => 'boolean',
    ];

    const COLORS = ['yellow', 'blue', 'green', 'pink', 'purple', 'gray'];

    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('content', 'like', "%{$term}%");
        });
    }

    /** Tailwind bg + text classes für die Notizfarbe */
    public function colorClasses(): array
    {
        return match ($this->color) {
            'yellow' => ['bg' => 'bg-amber-50',  'border' => 'border-amber-200',  'text' => 'text-amber-800'],
            'blue'   => ['bg' => 'bg-blue-50',   'border' => 'border-blue-200',   'text' => 'text-blue-800'],
            'green'  => ['bg' => 'bg-green-50',  'border' => 'border-green-200',  'text' => 'text-green-800'],
            'pink'   => ['bg' => 'bg-pink-50',   'border' => 'border-pink-200',   'text' => 'text-pink-800'],
            'purple' => ['bg' => 'bg-purple-50', 'border' => 'border-purple-200', 'text' => 'text-purple-800'],
            default  => ['bg' => 'bg-gray-50',   'border' => 'border-gray-200',   'text' => 'text-gray-800'],
        };
    }
}
