<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\AbaxisLab\Entities\Drivers\AbaxisDriver;

/**
 * Seed the Abaxis VetScan analyte catalog into LabCore's lab_analyte_map.
 * Idempotent — skips if Abaxis rows already exist.
 */
return new class extends Migration {
    public function up(): void
    {
        if (!Schema::hasTable('lab_analyte_map')) {
            return; // LabCore not installed — nothing to seed into
        }
        if (DB::table('lab_analyte_map')->where('vendor', 'abaxis')->exists()) {
            return; // already seeded
        }

        $now  = now();
        $rows = [];
        foreach ((new AbaxisDriver())->defaultAnalyteMap() as $r) {
            $rows[] = array_merge($r, [
                'business_id' => null,
                'vendor'      => 'abaxis',
                'model'       => null,
                'created_at'  => $now,
                'updated_at'  => $now,
            ]);
        }

        if ($rows) {
            DB::table('lab_analyte_map')->insert($rows);
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('lab_analyte_map')) {
            DB::table('lab_analyte_map')->where('vendor', 'abaxis')->delete();
        }
    }
};
