<?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 `reimbursement_batch_items` (
`id` int UNSIGNED NOT NULL,
  `batch_id` int UNSIGNED NOT NULL,
  `expense_id` int UNSIGNED NOT NULL,
  `reimbursement_id` int UNSIGNED DEFAULT NULL,
  `admin_id` int UNSIGNED DEFAULT NULL,
  `assigned_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `unassigned_at` datetime DEFAULT NULL,
  `notes` text COLLATE utf8mb4_general_ci,
  `school_year` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `semester` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `reimbursement_batch_items`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_batch_expense` (`batch_id`,`expense_id`),
  ADD KEY `idx_admin_id` (`admin_id`),
  ADD KEY `idx_batch` (`batch_id`),
  ADD KEY `fk_batch_items_expense` (`expense_id`),
  ADD KEY `fk_batch_items_reimbursement` (`reimbursement_id`);
SQL
        );

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

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `reimbursement_batch_items`
  ADD CONSTRAINT `fk_batch_items_admin` FOREIGN KEY (`admin_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_batch_items_batch` FOREIGN KEY (`batch_id`) REFERENCES `reimbursement_batches` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_batch_items_expense` FOREIGN KEY (`expense_id`) REFERENCES `expenses` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `fk_batch_items_reimbursement` FOREIGN KEY (`reimbursement_id`) REFERENCES `reimbursements` (`id`) ON DELETE SET NULL;
SQL
        );

    }

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