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

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


GRANT ALL PRIVILEGES ON adtlas_dev_db.* TO 'adtlas_dev_user'@'%';

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

-- Grant additional privileges for development
GRANT CREATE, DROP, ALTER, INDEX, REFERENCES ON *.* TO 'adtlas_dev_user'@'%';

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

-- Use the development database
USE adtlas_dev_db;

-- Development-specific MySQL configurations
SET GLOBAL innodb_buffer_pool_size = 134217728; -- 128MB (smaller for dev)
SET GLOBAL max_connections = 100;
SET GLOBAL query_cache_size = 33554432; -- 32MB
SET GLOBAL query_cache_type = 1;

-- Enable general query log for development debugging
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file = '/var/log/mysql/general.log';

-- Enable slow query log for performance debugging
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL slow_query_log_file = '/var/log/mysql/slow.log';
SET GLOBAL long_query_time = 1; -- Log queries taking more than 1 second

-- Set timezone

-- Optimize for development (faster but less durable)
SET GLOBAL innodb_file_per_table = 1;
SET GLOBAL innodb_flush_log_at_trx_commit = 0; -- Faster for development
SET GLOBAL innodb_log_buffer_size = 8388608; -- 8MB
SET GLOBAL sync_binlog = 0; -- Faster for development

-- Enable performance schema for debugging
SET GLOBAL performance_schema = ON;

-- Create some development-specific tables for testing
-- Note: Django migrations will handle most table creation

-- Sample data insertion can be done here if needed
-- INSERT INTO ... VALUES ...;

-- Show databases to confirm creation
SHOW DATABASES;

-- Show users to confirm creation
SELECT User, Host FROM mysql.user WHERE User LIKE 'adtlas%';