-- Manual Database Setup for Altibix Admin Panel
-- Run this SQL script directly on your MySQL database to create essential tables

-- Create admins table
CREATE TABLE IF NOT EXISTS `admins` (
    `id` CHAR(36) PRIMARY KEY,
    `email` VARCHAR(255) NOT NULL UNIQUE,
    `password_hash` VARCHAR(255) NOT NULL,
    `name` VARCHAR(255) NULL,
    `role` VARCHAR(50) NOT NULL DEFAULT 'support_admin',
    `is_active` TINYINT(1) NOT NULL DEFAULT 1,
    `permissions` JSON NULL,
    `last_login` TIMESTAMP NULL,
    `created_at` TIMESTAMP NULL,
    `updated_at` TIMESTAMP NULL,
    INDEX `admins_email_index` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create audit_logs table
CREATE TABLE IF NOT EXISTS `audit_logs` (
    `id` CHAR(36) PRIMARY KEY,
    `action` VARCHAR(255) NOT NULL,
    `table_name` VARCHAR(255) NULL,
    `record_id` VARCHAR(255) NULL,
    `record_title` VARCHAR(255) NULL,
    `old_data` JSON NULL,
    `new_data` JSON NULL,
    `performed_by` CHAR(36) NULL,
    `performed_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`performed_by`) REFERENCES `admins`(`id`) ON DELETE SET NULL,
    INDEX `audit_logs_performed_by_index` (`performed_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create a default super admin user
-- Email: admin@altibix.com
-- Password: admin123
-- Note: This is a bcrypt hash of 'admin123' - CHANGE THIS PASSWORD IMMEDIATELY IN PRODUCTION!
INSERT INTO `admins` (`id`, `email`, `password_hash`, `name`, `role`, `is_active`, `permissions`, `created_at`, `updated_at`)
VALUES (
    UUID(),
    'admin@altibix.com',
    '$2y$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5LoKxqE.CRhMC',
    'Super Admin',
    'super_admin',
    1,
    NULL,
    NOW(),
    NOW()
);

-- Mark the admin migrations as complete in the migrations table
INSERT INTO `migrations` (`migration`, `batch`) VALUES
('2024_01_02_000008_create_admin_tables', 1);

-- Mark the blog migration as complete (table already exists)
INSERT INTO `migrations` (`migration`, `batch`) VALUES
('2026_01_13_091410_create_blogs_table', 1);

SELECT 'Database setup complete!' AS Status;
SELECT email, name, role FROM admins;
