<?php
require __DIR__ . '/bootstrap.php';
use App\Core\Database;
try {
    $db = Database::getInstance();
    
    $sqlSettings = "
        CREATE TABLE IF NOT EXISTS `wedding_gift_settings` (
            `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            `invitation_id` BIGINT UNSIGNED NOT NULL COMMENT '-> invitations.id',
            `is_enabled` TINYINT(1) NOT NULL DEFAULT 1,
            `title` VARCHAR(150) NOT NULL DEFAULT 'Wedding Gift',
            `subtitle` TEXT NULL,
            `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            UNIQUE KEY `uk_invitation` (`invitation_id`),
            CONSTRAINT `fk_wg_settings_invitation` FOREIGN KEY (`invitation_id`) REFERENCES `invitations` (`id`) ON DELETE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    ";
    
    $sqlAccounts = "
        CREATE TABLE IF NOT EXISTS `wedding_gift_accounts` (
            `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            `gift_setting_id` BIGINT UNSIGNED NOT NULL COMMENT '-> wedding_gift_settings.id',
            `provider_name` VERCHAR(100) NOT NULL COMMENT 'Bank/Wallet Name e.g. BCA',
            `account_number` VARCHAR(100) NOT NULL,
            `account_holder` VARCHAR(150) NOT NULL,
            `account_type` VARCHAR(50) NOT NULL DEFAULT 'bank_transfer' COMMENT 'bank_transfer, qris, etc',
            `is_active` TINYINT(1) NOT NULL DEFAULT 1,
            `sort_order` MALLINT UNSIGNED NOT NULL DEFAULT 0,
            `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            CONSTRAINT `fk_wg_accounts_setting` FOREIGN KEY (`gift_setting_id`) REFERENCES `wedding_gift_settings` (`id`) ON DELETE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    ";
    $db->exec($sqlSettings);
    echo \"wedding_gift_settings table created successfully.\n\";
    $db->exec($sqlAccounts);
    echo \"wedding_gift_accounts table created successfully.\n\";
} catch (\Throwable $e) {
    echo \"Error: \" . $e->getMessage() . \"\n\";
}