<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Shift extends Model
{
    use BelongsToOwner, SoftDeletes;

    protected $table = 'care_shifts';

    protected $fillable = [
        'owner_ref',
        'organization_id',
        'code',
        'label',
        'start_time',
        'end_time',
        'color',
        'sort_order',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

    public function organization(): BelongsTo
    {
        return $this->belongsTo(Organization::class, 'organization_id');
    }

    public function rosterEntries(): HasMany
    {
        return $this->hasMany(RosterEntry::class, 'shift_id');
    }

    public function timeLabel(): string
    {
        $start = substr((string) $this->start_time, 0, 5);
        $end = substr((string) $this->end_time, 0, 5);

        return $start.'–'.$end;
    }
}
