-- Airline Tycoon Browser-MMO Datenbankschema (SQLite)
-- Phase 1: User, Airlines, Flughäfen, Basis-Routen

-- Benutzer (Spieler)
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    airline_name VARCHAR(100) DEFAULT '',
    balance DECIMAL(15,2) DEFAULT 500000.00,
    level INTEGER DEFAULT 1,
    experience INTEGER DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    last_login DATETIME DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN DEFAULT 1
);

-- Flughäfen (statisch, geladen aus CSV)
CREATE TABLE IF NOT EXISTS airports (
    id INTEGER PRIMARY KEY,
    iata_code VARCHAR(3) UNIQUE NOT NULL,
    name VARCHAR(200) NOT NULL,
    city VARCHAR(100) NOT NULL,
    country VARCHAR(100) NOT NULL,
    continent VARCHAR(50) NOT NULL,
    latitude DECIMAL(10,6) NOT NULL,
    longitude DECIMAL(10,6) NOT NULL,
    timezone VARCHAR(50) DEFAULT 'UTC',
    passenger_volume INTEGER DEFAULT 1000000,
    demand_multiplier DECIMAL(3,2) DEFAULT 1.00
);

-- Flugzeuge (Fleet)
CREATE TABLE IF NOT EXISTS aircraft (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    aircraft_type VARCHAR(50) NOT NULL,
    name VARCHAR(100) DEFAULT '',
    condition DECIMAL(5,2) DEFAULT 100.00,
    flights_count INTEGER DEFAULT 0,
    purchase_price DECIMAL(15,2) NOT NULL,
    seat_capacity INTEGER NOT NULL,
    range_km INTEGER NOT NULL,
    fuel_efficiency DECIMAL(5,2) DEFAULT 1.00,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

-- Routen
CREATE TABLE IF NOT EXISTS routes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    from_airport_id INTEGER NOT NULL,
    to_airport_id INTEGER NOT NULL,
    distance_km INTEGER NOT NULL,
    base_price DECIMAL(10,2) DEFAULT 100.00,
    demand_level INTEGER DEFAULT 50,
    is_active BOOLEAN DEFAULT 1,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (from_airport_id) REFERENCES airports(id),
    FOREIGN KEY (to_airport_id) REFERENCES airports(id)
);

-- Flüge (durchgeführte Flüge)
CREATE TABLE IF NOT EXISTS flights (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    route_id INTEGER NOT NULL,
    aircraft_id INTEGER NOT NULL,
    user_id INTEGER NOT NULL,
    departure_time DATETIME NOT NULL,
    arrival_time DATETIME NOT NULL,
    passengers INTEGER DEFAULT 0,
    revenue DECIMAL(15,2) DEFAULT 0.00,
    fuel_cost DECIMAL(15,2) DEFAULT 0.00,
    profit DECIMAL(15,2) DEFAULT 0.00,
    status VARCHAR(20) DEFAULT 'completed',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (route_id) REFERENCES routes(id),
    FOREIGN KEY (aircraft_id) REFERENCES aircraft(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

-- Statistiken (tägliche Auswertungen)
CREATE TABLE IF NOT EXISTS daily_stats (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    date DATE NOT NULL,
    total_flights INTEGER DEFAULT 0,
    total_passengers INTEGER DEFAULT 0,
    total_revenue DECIMAL(15,2) DEFAULT 0.00,
    total_profit DECIMAL(15,2) DEFAULT 0.00,
    fuel_cost DECIMAL(15,2) DEFAULT 0.00,
    UNIQUE(user_id, date),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

-- Events (Werbeaktionen, Wettbewerbe)
CREATE TABLE IF NOT EXISTS events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR(200) NOT NULL,
    description TEXT,
    event_type VARCHAR(50) DEFAULT 'special',
    bonus_multiplier DECIMAL(3,2) DEFAULT 1.00,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    is_active BOOLEAN DEFAULT 1
);

-- Indexes für Performance
CREATE INDEX IF NOT EXISTS idx_routes_user ON routes(user_id);
CREATE INDEX IF NOT EXISTS idx_flights_user ON flights(user_id);
CREATE INDEX IF NOT EXISTS idx_flights_route ON flights(route_id);
CREATE INDEX IF NOT EXISTS idx_aircraft_user ON aircraft(user_id);
CREATE INDEX IF NOT EXISTS idx_airports_iata ON airports(iata_code);
