CREATE DATABASE IF NOT EXISTS multiapp_panel;
USE multiapp_panel;

CREATE TABLE admins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE apps (
    id INT AUTO_INCREMENT PRIMARY KEY,
    app_name VARCHAR(150) NOT NULL,
    package_name VARCHAR(200) NOT NULL,
    website_url VARCHAR(255) NOT NULL,
    firebase_project_id VARCHAR(200) NOT NULL,
    fcm_topic VARCHAR(150) NOT NULL,
    service_account_file VARCHAR(255) NOT NULL,
    is_active TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE app_sessions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    app_id INT NOT NULL,
    user_token VARCHAR(255) NOT NULL,
    last_seen DATETIME NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uniq_app_user (app_id, user_token),
    CONSTRAINT fk_app_sessions_app FOREIGN KEY (app_id) REFERENCES apps(id) ON DELETE CASCADE
);

-- username: admin
-- password: admin123
INSERT INTO admins (username, password)
VALUES (
    'admin',
    '$2y$10$KIXk1uQnM2n6xU9Xv7ZkUO.Z9YQ6uF5f2P2jM1gBq4n9Jf4nC2m7S'
);