<?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 `homework` (
`id` int UNSIGNED NOT NULL,
  `student_id` int NOT NULL,
  `school_id` char(36) NOT NULL,
  `class_section_id` int DEFAULT NULL,
  `updated_by` int NOT NULL,
  `homework_index` tinyint DEFAULT NULL,
  `score` float DEFAULT NULL,
  `comment` text,
  `semester` varchar(25) NOT NULL,
  `school_year` varchar(9) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- --------------------------------------------------------

--
-- Table structure for table `inventory_categories`
--


CREATE TABLE `inventory_categories` (

`id` int UNSIGNED NOT NULL,

  `type` enum('classroom','book','office','kitchen') NOT NULL DEFAULT 'office',

  `name` varchar(120) NOT NULL,

  `description` text,

  `grade_min` tinyint UNSIGNED DEFAULT NULL,

  `grade_max` tinyint UNSIGNED DEFAULT NULL,

  `created_at` datetime DEFAULT NULL,

  `updated_at` datetime DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- --------------------------------------------------------

--
-- Table structure for table `inventory_items`
--


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;

-- --------------------------------------------------------

--
-- Table structure for table `inventory_movements`
--


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;

-- --------------------------------------------------------

--
-- Table structure for table `invoices`
--


CREATE TABLE `invoices` (

`id` int UNSIGNED NOT NULL,

  `parent_id` int NOT NULL,

  `invoice_number` varchar(50) COLLATE utf8mb4_general_ci NOT NULL,

  `total_amount` decimal(10,2) NOT NULL DEFAULT '0.00',

  `balance` decimal(10,2) DEFAULT '0.00',

  `paid_amount` decimal(10,2) NOT NULL DEFAULT '0.00',

  `has_discount` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1 if discount applied, 0 no discount',

  `issue_date` date NOT NULL,

  `due_date` date DEFAULT NULL,

  `status` varchar(50) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '''unpaid''',

  `description` text COLLATE utf8mb4_general_ci,

  `school_year` varchar(9) COLLATE utf8mb4_general_ci DEFAULT NULL,

  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,

  `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,

  `updated_by` int 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 `homework`
  ADD PRIMARY KEY (`id`);
SQL
        );

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

    }

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