<?php

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

class CreateVaCareTaskTypesTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('va_care_task_types')) return;

        Schema::create('va_care_task_types', function (Blueprint $table) {
            $table->increments('id');
            // null business_id = system default available to every clinic
            $table->unsignedInteger('business_id')->nullable();
            $table->string('name', 100);
            $table->enum('category', [
                'feeding', 'hydration', 'exercise', 'medication',
                'hygiene', 'vitals', 'observation', 'other',
            ])->default('other');
            $table->string('icon', 40)->default('fa-clipboard-check');
            $table->string('color', 20)->default('#3b82f6');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->timestamps();

            $table->index('business_id');
        });

        $now = now();
        // Font Awesome 5 icon names (the ERP ships FA 5.15 — solid set only).
        $defaults = [
            ['Feeding',              'feeding',     'fa-utensils',           '#f59e0b', 1],
            ['Water / Hydration',    'hydration',   'fa-tint',               '#0ea5e9', 2],
            ['Walk / Exercise',      'exercise',    'fa-walking',            '#22c55e', 3],
            ['Cage / Kennel Clean',  'hygiene',     'fa-broom',              '#8b5cf6', 4],
            ['Litter Change',        'hygiene',     'fa-trash',              '#a855f7', 5],
            ['Medication',           'medication',  'fa-pills',              '#ef4444', 6],
            ['Temperature Check',    'vitals',      'fa-thermometer-half',   '#f43f5e', 7],
            ['Weight Check',         'vitals',      'fa-weight',             '#06b6d4', 8],
            ['Wound Dressing',       'hygiene',     'fa-band-aid',           '#fb923c', 9],
            ['General Observation',  'observation', 'fa-eye',                '#64748b', 10],
        ];

        foreach ($defaults as $d) {
            DB::table('va_care_task_types')->insert([
                'business_id' => null,
                'name'        => $d[0],
                'category'    => $d[1],
                'icon'        => $d[2],
                'color'       => $d[3],
                'sort_order'  => $d[4],
                'status'      => 'active',
                'created_at'  => $now,
                'updated_at'  => $now,
            ]);
        }
    }

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