-- POLASH FF CLUB database schema
-- Create your database in cPanel first, then select it in phpMyAdmin and import this file.
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS polash_match_results,polash_point_history,polash_match_entries,polash_matches,polash_sessions,polash_users;
SET FOREIGN_KEY_CHECKS=1;

CREATE TABLE polash_users (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 username VARCHAR(32) NOT NULL UNIQUE,
 email VARCHAR(190) NOT NULL UNIQUE,
 mobile VARCHAR(25) NULL,
 password_hash VARCHAR(255) NOT NULL,
 avatar VARCHAR(500) NULL,
 points INT NOT NULL DEFAULT 0,
 wins INT UNSIGNED NOT NULL DEFAULT 0,
 role ENUM('user','admin') NOT NULL DEFAULT 'user',
 status ENUM('active','blocked') NOT NULL DEFAULT 'active',
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE polash_sessions (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 user_id BIGINT UNSIGNED NOT NULL,
 token_hash CHAR(64) NOT NULL UNIQUE,
 expires_at DATETIME NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(user_id), INDEX(expires_at),
 CONSTRAINT fk_polash_session_user FOREIGN KEY(user_id) REFERENCES polash_users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE polash_matches (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(120) NOT NULL,
 mode VARCHAR(60) NOT NULL,
 map_name VARCHAR(60) NULL,
 team_size TINYINT UNSIGNED NOT NULL DEFAULT 1,
 entry_points INT UNSIGNED NOT NULL DEFAULT 0,
 prize_points INT UNSIGNED NOT NULL DEFAULT 0,
 max_players INT UNSIGNED NOT NULL DEFAULT 48,
 joined_players INT UNSIGNED NOT NULL DEFAULT 0,
 start_time DATETIME NOT NULL,
 status ENUM('upcoming','live','completed','cancelled') NOT NULL DEFAULT 'upcoming',
 rules TEXT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(status,start_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE polash_match_entries (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 match_id BIGINT UNSIGNED NOT NULL,
 user_id BIGINT UNSIGNED NOT NULL,
 joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY unique_match_user(match_id,user_id),
 CONSTRAINT fk_polash_entry_match FOREIGN KEY(match_id) REFERENCES polash_matches(id) ON DELETE CASCADE,
 CONSTRAINT fk_polash_entry_user FOREIGN KEY(user_id) REFERENCES polash_users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE polash_match_results (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 match_id BIGINT UNSIGNED NOT NULL,
 user_id BIGINT UNSIGNED NOT NULL,
 rank_position INT UNSIGNED NOT NULL,
 reward_points INT UNSIGNED NOT NULL DEFAULT 0,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY unique_match_result(match_id,user_id),
 CONSTRAINT fk_polash_result_match FOREIGN KEY(match_id) REFERENCES polash_matches(id) ON DELETE CASCADE,
 CONSTRAINT fk_polash_result_user FOREIGN KEY(user_id) REFERENCES polash_users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE polash_point_history (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 user_id BIGINT UNSIGNED NOT NULL,
 points INT NOT NULL,
 action ENUM('match_entry','match_reward','admin_adjustment') NOT NULL,
 description VARCHAR(255) NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(user_id,created_at),
 CONSTRAINT fk_polash_points_user FOREIGN KEY(user_id) REFERENCES polash_users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
