-- Database initialization script for A/D Infrastructure Control

-- Services table for controller
CREATE TABLE IF NOT EXISTS services (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL UNIQUE,
    path VARCHAR(512) NOT NULL,
    git_url VARCHAR(512),
    alias VARCHAR(255),
    status VARCHAR(50) DEFAULT 'stopped',
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Service logs table
CREATE TABLE IF NOT EXISTS service_logs (
    id SERIAL PRIMARY KEY,
    service_id INTEGER REFERENCES services(id) ON DELETE CASCADE,
    action VARCHAR(100) NOT NULL,
    status VARCHAR(50) NOT NULL,
    message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Scoreboard state table - current flags count for each team+service
CREATE TABLE IF NOT EXISTS scoreboard_state (
    id SERIAL PRIMARY KEY,
    team_id INTEGER NOT NULL,
    service_name VARCHAR(255) NOT NULL,
    stolen_flags INTEGER DEFAULT 0,
    lost_flags INTEGER DEFAULT 0,
    fp_score FLOAT DEFAULT 0,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(team_id, service_name)
);

-- Team scores table - track total and flag points over time
CREATE TABLE IF NOT EXISTS team_scores (
    id SERIAL PRIMARY KEY,
    team_id INTEGER NOT NULL,
    team_name VARCHAR(255) NOT NULL,
    total_score FLOAT DEFAULT 0,
    flag_points FLOAT DEFAULT 0,
    round INTEGER NOT NULL,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Attacks tracking table for scoreboard injector
CREATE TABLE IF NOT EXISTS attacks (
    id SERIAL PRIMARY KEY,
    attack_id VARCHAR(255) UNIQUE,
    attacker_team_id INTEGER,
    victim_team_id INTEGER,
    service_name VARCHAR(255),
    flag VARCHAR(255),
    timestamp TIMESTAMP NOT NULL,
    points FLOAT,
    is_our_attack BOOLEAN DEFAULT FALSE,
    is_attack_to_us BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Attack alerts table
CREATE TABLE IF NOT EXISTS attack_alerts (
    id SERIAL PRIMARY KEY,
    attack_id INTEGER REFERENCES attacks(id) ON DELETE CASCADE,
    alert_type VARCHAR(100) NOT NULL,
    severity VARCHAR(50) NOT NULL,
    message TEXT,
    notified BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Telegram messages log
CREATE TABLE IF NOT EXISTS telegram_messages (
    id SERIAL PRIMARY KEY,
    chat_id BIGINT NOT NULL,
    message TEXT NOT NULL,
    sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    success BOOLEAN DEFAULT TRUE,
    error_message TEXT
);

-- System settings
CREATE TABLE IF NOT EXISTS settings (
    key VARCHAR(255) PRIMARY KEY,
    value TEXT NOT NULL,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Insert default settings
INSERT INTO settings (key, value) VALUES 
    ('our_team_id', '0'),
    ('alert_threshold_points', '100'),
    ('alert_threshold_time', '300')
ON CONFLICT (key) DO NOTHING;

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_scoreboard_state_team_service ON scoreboard_state(team_id, service_name);
CREATE INDEX IF NOT EXISTS idx_team_scores_round ON team_scores(round);
CREATE INDEX IF NOT EXISTS idx_team_scores_team_id ON team_scores(team_id);
CREATE INDEX IF NOT EXISTS idx_attacks_timestamp ON attacks(timestamp);
CREATE INDEX IF NOT EXISTS idx_attacks_our_attack ON attacks(is_our_attack);
CREATE INDEX IF NOT EXISTS idx_attacks_attack_to_us ON attacks(is_attack_to_us);
CREATE INDEX IF NOT EXISTS idx_service_logs_service_id ON service_logs(service_id);
CREATE INDEX IF NOT EXISTS idx_attack_alerts_notified ON attack_alerts(notified);
