-- WhaleTown V2 core social infrastructure. Run after the existing users/user_profiles tables.
-- Compatible with MySQL 5.7+/MariaDB: add the column only once.
SET @nickname_column_exists := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users' AND COLUMN_NAME = 'nickname_updated_at'
);
SET @nickname_column_sql := IF(
  @nickname_column_exists = 0,
  'ALTER TABLE users ADD COLUMN nickname_updated_at DATETIME NULL COMMENT ''社区昵称最近修改时间''',
  'SELECT 1'
);
PREPARE nickname_column_statement FROM @nickname_column_sql;
EXECUTE nickname_column_statement;
DEALLOCATE PREPARE nickname_column_statement;
UPDATE user_profiles SET current_map = 'whale_port' WHERE current_map = 'plaza';
UPDATE user_profiles SET current_map = 'personal_space' WHERE current_map = 'room';

CREATE TABLE IF NOT EXISTS friendships (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_low_id BIGINT NOT NULL,
  user_high_id BIGINT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_friendships_pair (user_low_id, user_high_id),
  KEY idx_friendships_low (user_low_id),
  KEY idx_friendships_high (user_high_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS friend_requests (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  requester_id BIGINT NOT NULL,
  recipient_id BIGINT NOT NULL,
  status VARCHAR(16) NOT NULL DEFAULT 'pending',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  expires_at DATETIME NOT NULL,
  responded_at DATETIME NULL,
  KEY idx_friend_requests_recipient (recipient_id, status, expires_at),
  KEY idx_friend_requests_pair (requester_id, recipient_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS user_blocks (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  blocked_user_id BIGINT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_user_blocks_pair (user_id, blocked_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS direct_messages (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  sender_id BIGINT NOT NULL,
  recipient_id BIGINT NOT NULL,
  content VARCHAR(1000) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  expires_at DATETIME NOT NULL,
  read_at DATETIME NULL,
  KEY idx_direct_messages_conversation (sender_id, recipient_id, created_at),
  KEY idx_direct_messages_unread (recipient_id, read_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS user_reports (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  reporter_id BIGINT NOT NULL,
  reported_user_id BIGINT NOT NULL,
  reason VARCHAR(32) NOT NULL,
  note VARCHAR(500) NULL,
  message_id BIGINT NULL,
  status VARCHAR(24) NOT NULL DEFAULT 'received',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_user_reports_reporter (reporter_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS player_travel_unlocks (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  destination_id VARCHAR(80) NOT NULL,
  discovered_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_player_travel_unlocks (user_id, destination_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS social_notifications (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  category VARCHAR(48) NOT NULL,
  title VARCHAR(100) NOT NULL,
  content VARCHAR(500) NOT NULL,
  action_metadata JSON NULL,
  read_at DATETIME NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  expires_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_social_notifications_user (user_id, read_at, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
