<?php

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

return new class extends Migration
{
    public function up(): void
    {
        App\Support\SqliteCompat::statement(<<<'SQL'
CREATE TABLE `inventory_items` (
`id` int UNSIGNED NOT NULL,
  `type` enum('classroom','book','office','kitchen') NOT NULL,
  `category_id` int UNSIGNED DEFAULT NULL,
  `name` varchar(190) NOT NULL,
  `description` text,
  `quantity` int UNSIGNED NOT NULL DEFAULT '0',
  `good_qty` int UNSIGNED NOT NULL DEFAULT '0',
  `needs_repair_qty` int UNSIGNED NOT NULL DEFAULT '0',
  `need_replace_qty` int UNSIGNED NOT NULL DEFAULT '0',
  `cannot_find_qty` int UNSIGNED NOT NULL DEFAULT '0',
  `unit` varchar(32) DEFAULT NULL,
  `condition` enum('good','needs_repair','need_replace','cannot_find') DEFAULT NULL,
  `isbn` varchar(32) DEFAULT NULL,
  `edition` varchar(32) DEFAULT NULL,
  `sku` varchar(64) DEFAULT NULL,
  `notes` text,
  `semester` enum('Spring','Fall') DEFAULT NULL,
  `school_year` varchar(16) DEFAULT NULL,
  `updated_by` int UNSIGNED DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `inventory_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `type_name` (`type`,`name`),
  ADD KEY `school_year_semester` (`school_year`,`semester`),
  ADD KEY `category_id` (`category_id`),
  ADD KEY `updated_by` (`updated_by`),
  ADD KEY `idx_items_schoolyear_semester` (`school_year`,`semester`);
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `inventory_items`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=34;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `inventory_items`
  ADD CONSTRAINT `inventory_items_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `inventory_categories` (`id`) ON DELETE CASCADE ON UPDATE SET NULL;
SQL
        );

    }

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