-- FanLeague Arena v1.3 Full Feeds, Automated News, AI League Builder, and Community Enhancements.
-- Safe to run through public/upgrade/v1_3.php. Duplicate column/key warnings are ignored by the upgrader.

ALTER TABLE sports_news ADD COLUMN external_id VARCHAR(150) NULL AFTER source_id;
ALTER TABLE sports_news ADD COLUMN source_provider VARCHAR(80) NULL AFTER external_id;
ALTER TABLE sports_news ADD COLUMN source_name VARCHAR(191) NULL AFTER source_provider;
ALTER TABLE sports_news ADD COLUMN discussion_prompt VARCHAR(255) NULL AFTER impact_tag;
ALTER TABLE sports_news ADD COLUMN raw_payload JSON NULL AFTER discussion_prompt;
ALTER TABLE sports_news ADD UNIQUE KEY unique_news_external (sport, external_id);

ALTER TABLE fantasy_leagues ADD COLUMN ai_optimized TINYINT(1) DEFAULT 0 AFTER league_theme;
ALTER TABLE fantasy_leagues ADD COLUMN ai_config_json JSON NULL AFTER ai_optimized;
ALTER TABLE fantasy_leagues ADD COLUMN scoring_preset VARCHAR(100) DEFAULT 'standard' AFTER scoring_type;
ALTER TABLE fantasy_leagues ADD COLUMN weekly_lock_day VARCHAR(20) NULL AFTER scoring_preset;
ALTER TABLE fantasy_leagues ADD COLUMN allow_auto_lineup TINYINT(1) DEFAULT 0 AFTER weekly_lock_day;

CREATE TABLE IF NOT EXISTS game_feeds (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    game_id BIGINT UNSIGNED NOT NULL,
    sport ENUM('nba','nhl','mlb','mls') NOT NULL,
    external_id VARCHAR(100) NOT NULL,
    feed_type VARCHAR(50) DEFAULT 'summary',
    headline VARCHAR(255) NULL,
    summary TEXT NULL,
    source_url VARCHAR(500) NULL,
    payload_json JSON NULL,
    fetched_at DATETIME NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    UNIQUE KEY unique_game_feed_type (game_id, feed_type),
    INDEX idx_game_feeds_sport (sport, fetched_at),
    CONSTRAINT fk_game_feeds_game FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_feed_leaders (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    game_id BIGINT UNSIGNED NOT NULL,
    sport ENUM('nba','nhl','mlb','mls') NOT NULL,
    category VARCHAR(100) NOT NULL,
    player_name VARCHAR(191) NOT NULL,
    display_value VARCHAR(191) NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    UNIQUE KEY unique_game_leader (game_id, category, player_name),
    CONSTRAINT fk_game_leaders_game FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS league_ai_suggestions (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT UNSIGNED NOT NULL,
    sport ENUM('nba','nhl','mlb','mls') NOT NULL,
    suggested_name VARCHAR(191) NOT NULL,
    config_json JSON NOT NULL,
    accepted_at DATETIME NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    INDEX idx_ai_suggestions_user (user_id, created_at),
    CONSTRAINT fk_ai_suggestions_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS community_room_members (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    room_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    role ENUM('member','moderator','owner') DEFAULT 'member',
    joined_at DATETIME NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    UNIQUE KEY unique_room_user (room_id, user_id),
    CONSTRAINT fk_room_members_room FOREIGN KEY (room_id) REFERENCES community_rooms(id) ON DELETE CASCADE,
    CONSTRAINT fk_room_members_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS community_bookmarks (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    post_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    created_at TIMESTAMP NULL,
    UNIQUE KEY unique_bookmark (post_id, user_id),
    CONSTRAINT fk_bookmarks_post FOREIGN KEY (post_id) REFERENCES community_posts(id) ON DELETE CASCADE,
    CONSTRAINT fk_bookmarks_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS community_reports (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    post_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    reason VARCHAR(191) NOT NULL,
    notes TEXT NULL,
    status ENUM('open','reviewed','dismissed','actioned') DEFAULT 'open',
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    INDEX idx_reports_status (status, created_at),
    CONSTRAINT fk_reports_post FOREIGN KEY (post_id) REFERENCES community_posts(id) ON DELETE CASCADE,
    CONSTRAINT fk_reports_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO settings (setting_key, setting_value, created_at, updated_at) VALUES
('espn_full_feed_enabled','true',NOW(),NOW()),
('espn_news_auto_sync_enabled','true',NOW(),NOW()),
('ai_league_builder_enabled','true',NOW(),NOW()),
('community_rooms_enabled','true',NOW(),NOW()),
('v1_3_upgrade_installed_at',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s'),NOW(),NOW())
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value), updated_at = NOW();

INSERT INTO community_rooms (sport, name, slug, description, is_active, created_at, updated_at) VALUES
('nba','NBA Fantasy Lab','nba-fantasy-lab','Hoops trends, lineups, injuries, starts, sits, and waiver talk.',1,NOW(),NOW()),
('nhl','NHL Puck Room','nhl-puck-room','Goalies, power play movement, hits, blocks, and weekly fantasy debates.',1,NOW(),NOW()),
('mlb','MLB Diamond Room','mlb-diamond-room','Pitching streamers, hot bats, lineups, and matchup planning.',1,NOW(),NOW()),
('mls','MLS Matchday Lab','mls-matchday-lab','Starting XI watch, clean sheets, set pieces, and soccer fantasy-style decisions.',1,NOW(),NOW())
ON DUPLICATE KEY UPDATE description = VALUES(description), updated_at = NOW();
