<?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 `payment_notification_logs` (
`id` int UNSIGNED NOT NULL,
  `parent_id` int UNSIGNED NOT NULL,
  `invoice_id` int UNSIGNED DEFAULT NULL,
  `school_year` varchar(32) DEFAULT NULL,
  `period_year` smallint NOT NULL,
  `period_month` tinyint NOT NULL,
  `type` enum('no_payment','installment') NOT NULL DEFAULT 'installment',
  `to_email` varchar(191) DEFAULT NULL,
  `cc_email` varchar(191) DEFAULT NULL,
  `head_fa_notified` tinyint(1) NOT NULL DEFAULT '0',
  `subject` varchar(255) DEFAULT NULL,
  `body` mediumtext,
  `status` enum('sent','failed') NOT NULL DEFAULT 'sent',
  `error_message` text,
  `balance_snapshot` decimal(10,2) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `sent_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

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

--
-- Table structure for table `payment_transactions`
--


CREATE TABLE `payment_transactions` (

`id` int UNSIGNED NOT NULL,

  `transaction_id` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,

  `payment_id` int NOT NULL,

  `transaction_date` datetime DEFAULT CURRENT_TIMESTAMP,

  `amount` decimal(10,2) NOT NULL,

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

  `payment_status` varchar(50) COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'Pending',

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

  `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 `payment_notification_logs`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `parent_id_period_year_period_month_type` (`parent_id`,`period_year`,`period_month`,`type`),
  ADD KEY `parent_id` (`parent_id`),
  ADD KEY `invoice_id` (`invoice_id`),
  ADD KEY `school_year` (`school_year`),
  ADD KEY `period_year_period_month` (`period_year`,`period_month`);
SQL
        );

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

    }

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