<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DailyPlan extends Model
{
    protected $guarded = ['id'];

    protected $casts = [
        'plan_date' => 'date',
        'done_at' => 'datetime',
    ];

    protected $appends = ['is_overdue'];

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

    /** A pending plan whose date (or today's due time) has passed. */
    public function getIsOverdueAttribute(): bool
    {
        if ($this->status === 'done' || ! $this->plan_date) {
            return false;
        }
        if ($this->plan_date->isBefore(today())) {
            return true;
        }
        if ($this->plan_date->isSameDay(today()) && $this->due_time) {
            return now()->format('H:i') > substr($this->due_time, 0, 5);
        }

        return false;
    }
}
