<?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
    {
        if (Schema::hasTable('print_requests')) {
            return;
        }

        App\Support\SqliteCompat::statement(<<<'SQL'
CREATE TABLE `print_requests` (
`id` int UNSIGNED NOT NULL,
  `teacher_id` int UNSIGNED NOT NULL,
  `admin_id` int UNSIGNED DEFAULT NULL,
  `class_id` int UNSIGNED NOT NULL,
  `file_path` varchar(255) NOT NULL,
  `page_selection` varchar(255) DEFAULT NULL,
  `num_copies` int NOT NULL,
  `required_by` datetime NOT NULL,
  `pickup_method` varchar(255) NOT NULL,
  `status` enum('not_assigned','assigned','done','delivered') NOT NULL DEFAULT 'not_assigned',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
SQL
        );


        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `print_requests`
  ADD PRIMARY KEY (`id`),
  ADD KEY `print_requests_teacher_id_foreign` (`teacher_id`),
  ADD KEY `print_requests_admin_id_foreign` (`admin_id`),
  ADD KEY `print_requests_class_id_foreign` (`class_id`);
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `print_requests`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
SQL
        );

        App\Support\SqliteCompat::statement(<<<'SQL'
ALTER TABLE `print_requests`
  ADD CONSTRAINT `print_requests_admin_id_foreign` FOREIGN KEY (`admin_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  ADD CONSTRAINT `print_requests_teacher_id_foreign` FOREIGN KEY (`teacher_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
SQL
        );

    }

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