<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * medwf_shift_templates — reusable clinical shift definitions (Morning / Evening
 * / Night / On-call) that the duty-roster board paints onto staff-days.
 *
 * This is the MEDICAL layer on top of Essentials HR. Essentials already owns the
 * plain HR shift (essentials_shifts) used for attendance/auto-clockout; these
 * templates add the roster-specific fields the SIFA hr_payroll spec asks for
 * (department/location scoping, break minutes, night allowance, a display
 * colour) without touching the Essentials tables. RosterService optionally
 * mirrors a template into essentials_shifts so attendance keeps working.
 *
 * business_id-scoped. References into ClinicCore (clc_departments) and the ERP
 * core (business_locations) are plain indexed columns — no cross-table FK
 * constraints — matching the existing module convention (see Modules\MedBilling).
 */
return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('medwf_shift_templates')) {
            return;
        }

        Schema::create('medwf_shift_templates', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedInteger('business_id');
            $t->string('name', 100);                                  // Morning / Evening / Night / On-call
            $t->time('start_time');
            $t->time('end_time');
            $t->unsignedInteger('department_id')->nullable();          // clc_departments.id
            $t->unsignedInteger('business_location_id')->nullable();   // business_locations.id
            $t->integer('break_minutes')->default(0);
            $t->boolean('night_allowance')->default(false);
            $t->string('color', 20)->nullable();                       // hex badge colour for the board
            $t->boolean('is_active')->default(true);
            $t->timestamps();

            $t->index(['business_id', 'is_active'], 'medwf_tmpl_biz_active_idx');
            $t->index(['business_id', 'department_id'], 'medwf_tmpl_biz_dept_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('medwf_shift_templates');
    }
};
