<?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_movements` (
`id` int UNSIGNED NOT NULL,
  `item_id` int UNSIGNED NOT NULL,
  `qty_change` int NOT NULL,
  `movement_type` enum('initial','in','out','adjust','distribution') NOT NULL DEFAULT 'adjust',
  `reason` varchar(120) DEFAULT NULL,
  `note` text,
  `semester` enum('Spring','Fall') DEFAULT NULL,
  `school_year` varchar(16) DEFAULT NULL,
  `performed_by` int UNSIGNED DEFAULT NULL,
  `teacher_id` int UNSIGNED DEFAULT NULL,
  `student_id` int UNSIGNED DEFAULT NULL,
  `class_section_id` 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_movements`
  ADD PRIMARY KEY (`id`),
  ADD KEY `item_id` (`item_id`),
  ADD KEY `school_year_semester` (`school_year`,`semester`),
  ADD KEY `movement_type` (`movement_type`);
SQL
        );

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

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `inventory_movements`
  ADD CONSTRAINT `inventory_movements_item_id_foreign` FOREIGN KEY (`item_id`) REFERENCES `inventory_items` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
SQL
        );

    }

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