<?php

namespace Modules\MedWorkforce\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * A reusable clinical shift definition painted onto the duty-roster board
 * (SIFA P7-T1). The medical layer over Essentials HR — see the
 * create_medwf_shift_templates migration.
 */
class ShiftTemplate extends Model
{
    protected $table = 'medwf_shift_templates';

    protected $guarded = ['id'];

    protected $casts = [
        'break_minutes'   => 'integer',
        'night_allowance' => 'boolean',
        'is_active'       => 'boolean',
    ];

    /** Default badge colours cycled through when a template has no explicit colour. */
    public const DEFAULT_COLORS = ['#3c8dbc', '#00a65a', '#605ca8', '#f39c12', '#dd4b39', '#00c0ef'];

    public function assignments()
    {
        return $this->hasMany(RosterAssignment::class, 'shift_template_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /** A safe badge colour, falling back to a stable pick derived from the id. */
    public function badgeColor(): string
    {
        if (! empty($this->color)) {
            return $this->color;
        }

        return self::DEFAULT_COLORS[(int) $this->id % count(self::DEFAULT_COLORS)];
    }

    /** "08:00 – 16:00" for the board legend / cell tooltip. */
    public function timeLabel(): string
    {
        return substr((string) $this->start_time, 0, 5) . ' – ' . substr((string) $this->end_time, 0, 5);
    }
}
