-- Create the database if it doesn't exist
CREATE DATABASE IF NOT EXISTS proxmox_dashboard;
USE proxmox_dashboard;

-- Create servers table
CREATE TABLE IF NOT EXISTS servers (
    id VARCHAR(36) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    model VARCHAR(255) NOT NULL,
    cpu_model VARCHAR(255) NOT NULL,
    cpu_cores INT NOT NULL,
    cpu_count INT NOT NULL,
    ram_gb INT NOT NULL,
    proxmox_url VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_created_at (created_at),
    INDEX idx_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create a user for the application (if not exists)
CREATE USER IF NOT EXISTS 'proxmox_user'@'localhost' IDENTIFIED BY 'proxmox_password';

-- Grant privileges to the application user
GRANT ALL PRIVILEGES ON proxmox_dashboard.* TO 'proxmox_user'@'localhost';
FLUSH PRIVILEGES;

-- Add some sample data (optional)
INSERT INTO servers (
    id,
    name,
    model,
    cpu_model,
    cpu_cores,
    cpu_count,
    ram_gb,
    proxmox_url
) VALUES (
    UUID(),
    'Test Server 1',
    'Dell R720',
    'Intel Xeon E5-2680 v2',
    10,
    2,
    128,
    'https://proxmox1.example.com:8006'
);