-- =========================
-- TABLES
-- =========================


CREATE TABLE `users` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `email` VARCHAR(255) NOT NULL UNIQUE,
    `username` VARCHAR(32) NOT NULL UNIQUE,
    `balance` INT NOT NULL DEFAULT 0,
    `register_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `login_date` DATETIME,
    `ip` VARCHAR(45),
    `rank` VARCHAR(32) NOT NULL DEFAULT 'user',
    `credit` INT NOT NULL DEFAULT 0,
    `damage` INT NOT NULL DEFAULT 20,
    `max_damage_power` INT NOT NULL DEFAULT 20,
    `last_damage_reload` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `total_withdrawn` INT NOT NULL DEFAULT 0,
    `referred_by` INT DEFAULT NULL,
    `referral_earnings` INT NOT NULL DEFAULT 0,
    `banned` TINYINT(1) NOT NULL DEFAULT 0,
    `preferred_currency` VARCHAR(8) NOT NULL DEFAULT 'LTC',
    `last_faucet_claim` DATETIME DEFAULT NULL,
    `deposit_balance` INT NOT NULL DEFAULT 0,
    FOREIGN KEY (`referred_by`) REFERENCES `users`(`id`)
);


CREATE TABLE `monsters` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(255) NOT NULL,
    `image_path` VARCHAR(255) NOT NULL,
    `hp` INT NOT NULL,
    `xp` INT NOT NULL,
    `reward` INT NOT NULL -- reward in tokens
);

CREATE TABLE `arena_active_monster` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `monster_id` INT NOT NULL,
    `hp` INT NOT NULL,
    `activated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `status` VARCHAR(16) NOT NULL DEFAULT 'active',
    FOREIGN KEY (`monster_id`) REFERENCES `monsters`(`id`)
);

CREATE TABLE `monster_attacks` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `monster_id` INT NOT NULL,
    `arena_id` INT NOT NULL,
    `user_id` INT NOT NULL,
    `damage` INT NOT NULL,
    FOREIGN KEY (`monster_id`) REFERENCES `monsters`(`id`),
    FOREIGN KEY (`arena_id`) REFERENCES `arena_active_monster`(`id`),
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);

CREATE TABLE `chat_messages` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `message` VARCHAR(200) NOT NULL,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);

-- WITHDRAWALS & DEPOSITS
CREATE TABLE `withdraw_history` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `address` VARCHAR(128) NOT NULL,
    `amount` DECIMAL(20,8) NOT NULL,
    `amount_tokens` INT NOT NULL DEFAULT 0,
    `currency` VARCHAR(8) NOT NULL,
    `payout_id` VARCHAR(64),
    `payout_user_hash` VARCHAR(64),
    `status` VARCHAR(16) NOT NULL,
    `message` VARCHAR(255),
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);

CREATE TABLE `withdraw_stats` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `total_withdrawn` BIGINT NOT NULL DEFAULT 0
);

CREATE TABLE `deposit_history` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `amount` DECIMAL(10,2) NOT NULL DEFAULT 0,
    `trans_id` VARCHAR(64) DEFAULT NULL,
    `status` ENUM('pending','credited','failed','cancelled') DEFAULT 'pending',
    `amount_usd` DECIMAL(10,2) NOT NULL DEFAULT 0,
    `marked` TINYINT(1) NOT NULL DEFAULT 0,
    `currency` VARCHAR(16) DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`),
    UNIQUE KEY (`trans_id`)
);

-- OFFERWALLS & PTC
CREATE TABLE `offerwall_history` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `trans_id` VARCHAR(64) NOT NULL,
    `reward` INT NOT NULL,
    `status` INT NOT NULL,
    `type` VARCHAR(32) DEFAULT 'offerwallmedia',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);

CREATE TABLE `ptc_options` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(64) NOT NULL,
    `cost_per_view` INT NOT NULL DEFAULT 10,
    `min_views` INT NOT NULL DEFAULT 10,
    `reward` INT NOT NULL DEFAULT 5,
    `duration` INT NOT NULL DEFAULT 10
);

CREATE TABLE `ptc_ads` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `title` VARCHAR(255) NOT NULL,
    `url` VARCHAR(255),
    `description` VARCHAR(500),
    `reward` INT NOT NULL DEFAULT 1,
    `duration` INT NOT NULL DEFAULT 10,
    `daily_limit` INT NOT NULL DEFAULT 1,
    `active` TINYINT(1) NOT NULL DEFAULT 1,
    `purchased_views` INT NOT NULL DEFAULT 0,
    `remaining_views` INT NOT NULL DEFAULT 0,
    `user_id` INT DEFAULT NULL,
    `cost_per_view` INT NOT NULL DEFAULT 10,
    `total_cost` INT NOT NULL DEFAULT 0,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `expired` TINYINT(1) NOT NULL DEFAULT 0,
    `option_id` INT DEFAULT NULL,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`option_id`) REFERENCES `ptc_options`(`id`)
);

CREATE TABLE `ptc_views` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `ad_id` INT NOT NULL,
    `reward` INT NOT NULL,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`ad_id`) REFERENCES `ptc_ads`(`id`)
);


-- CURRENCIES
CREATE TABLE `currencies` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `code` VARCHAR(16) NOT NULL UNIQUE,
    `name` VARCHAR(64) NOT NULL,
    `rate` DECIMAL(20,10) NOT NULL DEFAULT 0,
    `enabled` TINYINT(1) NOT NULL DEFAULT 1
);

-- DAILY STATS
CREATE TABLE `daily_stats` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `stat_date` DATE NOT NULL,
    `offerwallmedia_credits` INT NOT NULL DEFAULT 0,
    `bitcotasks_credits` INT NOT NULL DEFAULT 0,
    `offerwallme_credits` INT NOT NULL DEFAULT 0,
    `excentiv_credits` INT NOT NULL DEFAULT 0,
    `timewall_credits` INT NOT NULL DEFAULT 0,
    `user_registrations` INT NOT NULL DEFAULT 0,
    `user_logins` INT NOT NULL DEFAULT 0,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY (`stat_date`)
);

-- CONTACT
CREATE TABLE `contact_threads` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `email` VARCHAR(255) NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `closed` TINYINT(1) NOT NULL DEFAULT 0
);

CREATE TABLE `contact_messages` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `thread_id` INT NOT NULL,
    `sender` ENUM('user','admin') NOT NULL DEFAULT 'user',
    `message` TEXT NOT NULL,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`thread_id`) REFERENCES `contact_threads`(`id`)
);

-- CARDS & INVENTORY
CREATE TABLE `cards` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(128) NOT NULL,
    `description` VARCHAR(512),
    `image_path` VARCHAR(255),
    `rarity` ENUM('common','normal','rare','shop') NOT NULL DEFAULT 'common',
    `shop_only` TINYINT(1) NOT NULL DEFAULT 0,
    `max_level` INT NOT NULL DEFAULT 1,
    `price` INT NOT NULL DEFAULT 0,
    `upgrade_needed` INT NOT NULL DEFAULT 2,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE `card_rewards` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `card_id` INT NOT NULL,
    `level` INT NOT NULL DEFAULT 1,
    `reward_type` ENUM('balance','deposit','damage_boost') NOT NULL,
    `reward_value` INT NOT NULL DEFAULT 0,
    `duration_hours` INT DEFAULT NULL,
    FOREIGN KEY (`card_id`) REFERENCES `cards`(`id`)
);

CREATE TABLE `user_inventory` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `card_id` INT NOT NULL,
    `level` INT NOT NULL DEFAULT 1,
    `claimed` TINYINT(1) NOT NULL DEFAULT 0,
    `awarded_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `expires_at` DATETIME DEFAULT NULL,
    `active` TINYINT(1) NOT NULL DEFAULT 0,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`card_id`) REFERENCES `cards`(`id`)
);

CREATE TABLE `monster_card_awards` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `arena_id` INT NOT NULL,
    `winner_user_id` INT NOT NULL,
    `card_id` INT NOT NULL,
    `awarded_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `claimed` TINYINT(1) NOT NULL DEFAULT 0,
    FOREIGN KEY (`arena_id`) REFERENCES `arena_active_monster`(`id`),
    FOREIGN KEY (`winner_user_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`card_id`) REFERENCES `cards`(`id`)
);

CREATE TABLE `premium_plans` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(64) NOT NULL,
    `price` INT NOT NULL DEFAULT 0,
    `duration_days` INT NOT NULL DEFAULT 30,
    `hide_banners` TINYINT(1) NOT NULL DEFAULT 0,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE `user_premium` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `plan_id` INT NOT NULL,
    `start_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `end_date` DATETIME NOT NULL,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`plan_id`) REFERENCES `premium_plans`(`id`)
);

-- =========================
-- INITIAL DATA
-- =========================

-- MONSTERS
INSERT INTO `monsters` (`name`, `image_path`, `hp`, `xp`, `reward`) VALUES
('Cave Bat', '/images/cavebat.png', 120, 240, 120),
('Dark Goblin', '/images/darkgoblin.png', 100, 200, 100),
('Fire Beetle', '/images/firebeetle.png', 110, 220, 110),
('Fire Dragon', '/images/firedragon.png', 200, 400, 200),
('Forest Pixie', '/images/forestpixie.png', 130, 260, 130),
('Forest Troll', '/images/foresttroll.png', 150, 300, 150),
('Ice Wolf', '/images/icewolf.png', 140, 280, 140),
('Mud Crab', '/images/mudcrab.png', 160, 320, 160),
('Stone Golem', '/images/stonegolem.png', 120, 240, 120),
('Swamp Slime', '/images/swampslime.png', 180, 360, 180),
('Thunder Eagle', '/images/thundereagle.png', 100, 200, 100);

-- CURRENCIES
INSERT INTO `currencies` (`code`, `name`, `rate`, `enabled`) VALUES
('LTC', 'Litecoin', 0.00000008, 1),
('PEPE', 'Pepe', 0.7, 1),
('DGB', 'Digibyte', 0.001, 1);

-- =========================
-- SETTINGS
-- =========================

CREATE TABLE `settings` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(128) NOT NULL UNIQUE,
    `value` TEXT
);

INSERT INTO `settings` (`name`, `value`) VALUES
    ('site_name', 'Monster Games'),
    ('faucet_enabled', '1'),
    ('faucet_interval', '3600'),
    ('faucet_reward', '10'),
    ('faucet_banner_top_728', ''),
    ('faucet_banner_left_top_300', ''),
    ('faucet_banner_right_top_300', ''),
    ('faucet_banner_bottom_728', ''),
    ('ptc_enabled', '1'),
    ('shortlink_enabled', '1'),
    ('inventory_enabled', '1'),
    -- Shop
    ('shop_enabled', '1'),
    ('shop_max_damage_power_enabled', '1'),
    ('shop_max_damage_power_price', '1000'),
    ('shop_max_damage_power_limit', '40'),
    -- Withdraw
    ('payout_mode', 'instant'),
    ('faucetpay_api_key', ''),
    ('minimum_withdraw_tokens', '500'),
    ('withdraw_min_hours_between', '3'),
    ('withdraw_max_per_day', '3'),
    -- Deposit
    ('deposit_merchant_username', ''),
    ('deposit_currencies', ''),
    -- Offerwall settings - BitcoTasks
    ('bitcotasks_enabled', '0'),
    ('bitcotasks_api_key', ''),
    ('bitcotasks_secret', ''),
    -- Offerwall settings - OfferwallMedia
    ('offerwallmedia_enabled', '0'),
    ('offerwallmedia_api_key', ''),
    ('offerwallmedia_secret', ''),
    -- Offerwall settings - Offerwall.me
    ('offerwallme_enabled', '0'),
    ('offerwallme_api_key', ''),
    ('offerwallme_secret', ''),
    -- Proxy/VPN detection
    ('proxycheck_api_key', ''),
    -- Premium page enable/disable
    ('premium_enabled', '1'),
    -- Offerwall settings - Excentiv
    ('excentiv_enabled', '0'),
    ('excentiv_api_key', ''),
    ('excentiv_secret', ''),
    -- Offerwall settings - TimeWall
    ('timewall_enabled', '0'),
    ('timewall_api_key', ''),
    ('timewall_secret', ''),
    ('dashboard_banner_top_728', ''),
    ('dashboard_banner_between_728', ''),
    ('home_banner_top_728', ''),
    ('home_banner_bottom_728', ''),
    ('withdraw_daily_limit_tokens', '10000'),
    -- NEW: Require premium for withdraw
    ('withdraw_premium_required', '0'),
    ('withdraw_offerwall_required', '0'),
    ('withdraw_captcha_enabled', '1'),
    ('token_to_deposit_enabled', '1'),
    ('token_to_deposit_premium', '0'),
    ('credit_to_deposit_enabled', '1'),
    ('credit_to_deposit_premium', '0'),
    ('credit_to_balance_enabled', '1'),
    ('credit_to_balance_premium', '0'),
    ('credit_to_balance_rate', '100'),
    ('recaptcha_enabled_login', '1'),
    ('recaptcha_enabled_withdraw', '1'),
    ('recaptcha_site_key', ''),
    ('recaptcha_secret_key', '');


CREATE TABLE IF NOT EXISTS shortlinks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(128) NOT NULL,
    api_url TEXT NOT NULL,
    short_url TEXT,
    reward INT NOT NULL DEFAULT 0,
    daily_limit INT NOT NULL DEFAULT 1, -- pl. 1 vagy 20
    enabled TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE IF NOT EXISTS shortlink_clicks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    shortlink_id INT NOT NULL,
    claimed_at INT NOT NULL,
    ip_address VARCHAR(45) DEFAULT NULL,
    reward_given TINYINT(1) DEFAULT 0,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (shortlink_id) REFERENCES shortlinks(id)
);

CREATE TABLE IF NOT EXISTS shortlink_sessions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    shortlink_id INT NOT NULL,
    api_token VARCHAR(32) NOT NULL,
    short_url TEXT NOT NULL,
    created_at INT NOT NULL,
    UNIQUE KEY (api_token),
    INDEX (user_id),
    INDEX (shortlink_id)
);

CREATE TABLE IF NOT EXISTS shortlink_country_rewards (
    id INT AUTO_INCREMENT PRIMARY KEY,
    shortlink_id INT NOT NULL,
    country_code VARCHAR(8) NOT NULL,
    reward INT NOT NULL DEFAULT 0,
    daily_limit INT NOT NULL DEFAULT 1,
    FOREIGN KEY (shortlink_id) REFERENCES shortlinks(id)
);

ALTER TABLE premium_plans ADD COLUMN instant_withdraw TINYINT(1) NOT NULL DEFAULT 0;
ALTER TABLE premium_plans ADD COLUMN double_max_damage_power TINYINT(1) NOT NULL DEFAULT 0;
ALTER TABLE premium_plans ADD COLUMN disable_proxycheck TINYINT(1) NOT NULL DEFAULT 0;


CREATE TABLE IF NOT EXISTS proxy_checks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    ip VARCHAR(64) NOT NULL,
    checked_at DATETIME NOT NULL,
    result VARCHAR(32) NOT NULL,
    raw_response TEXT,
    INDEX(user_id),
    INDEX(ip),
    INDEX(result)
);

