-- MySQL initialization script for Adtlas project
-- This script will be executed when the MySQL container starts for the first time

-- Create the main database if it doesn't exist
CREATE DATABASE IF NOT EXISTS adtlas_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Create user and grant privileges
CREATE USER IF NOT EXISTS 'adtlas_user'@'%' IDENTIFIED BY 'adtlas_password';
GRANT ALL PRIVILEGES ON adtlas_db.* TO 'adtlas_user'@'%';

-- Create test database for testing
CREATE DATABASE IF NOT EXISTS test_adtlas_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON test_adtlas_db.* TO 'adtlas_user'@'%';

-- Flush privileges to ensure they take effect
FLUSH PRIVILEGES;

-- Use the main database
USE adtlas_db;

-- Create some initial tables that might be needed
-- Note: Django migrations will handle most table creation

-- Set some MySQL configurations for better performance
SET GLOBAL innodb_buffer_pool_size = 268435456; -- 256MB
SET GLOBAL max_connections = 200;
SET GLOBAL query_cache_size = 67108864; -- 64MB
SET GLOBAL query_cache_type = 1;

-- Enable binary logging for replication (optional)
-- SET GLOBAL log_bin = 'mysql-bin';
-- SET GLOBAL binlog_format = 'ROW';

-- Set timezone

-- Create indexes for better performance (these will be created by Django migrations)
-- But we can set some global settings here

-- Optimize for InnoDB
SET GLOBAL innodb_file_per_table = 1;
SET GLOBAL innodb_flush_log_at_trx_commit = 2;
SET GLOBAL innodb_log_buffer_size = 16777216; -- 16MB

-- Show databases to confirm creation
SHOW DATABASES;