-- Manual migration for cross-club friendly match concept
-- Created: 2026-05-30

-- 1) Invitation table
CREATE TABLE IF NOT EXISTS `friendly_match_invitation` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `from_club_id` INT NOT NULL,
  `to_club_id` INT NOT NULL,
  `proposed_date` DATE NOT NULL,
  `proposed_start_time` TIME NULL,
  `proposed_match_name` VARCHAR(255) NOT NULL,
  `message` TEXT NULL,
  `status` VARCHAR(32) NOT NULL DEFAULT 'pending',
  `created_by_user_id` INT NULL,
  `accepted_by_user_id` INT NULL,
  `accepted_at` DATETIME NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `chk_friendly_match_invitation_clubs_different`
    CHECK (`from_club_id` <> `to_club_id`),
  CONSTRAINT `fk_friendly_match_invitation_from_club`
    FOREIGN KEY (`from_club_id`) REFERENCES `clubs` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_friendly_match_invitation_to_club`
    FOREIGN KEY (`to_club_id`) REFERENCES `clubs` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_friendly_match_invitation_created_by`
    FOREIGN KEY (`created_by_user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_friendly_match_invitation_accepted_by`
    FOREIGN KEY (`accepted_by_user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL,
  KEY `idx_friendly_match_invitation_to_status_date` (`to_club_id`, `status`, `proposed_date`),
  KEY `idx_friendly_match_invitation_from_status_date` (`from_club_id`, `status`, `proposed_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 2) Shared match table
CREATE TABLE IF NOT EXISTS `friendly_match_shared` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `home_club_id` INT NOT NULL,
  `guest_club_id` INT NOT NULL,
  `date` DATE NOT NULL,
  `start_time` TIME NULL,
  `match_name` VARCHAR(255) NULL,
  `home_team_name` VARCHAR(255) NOT NULL,
  `guest_team_name` VARCHAR(255) NOT NULL,
  `location_name` VARCHAR(255) NULL,
  `location_address` VARCHAR(255) NULL,
  `location_city` VARCHAR(255) NULL,
  `location_zip` VARCHAR(32) NULL,
  `match_system` VARCHAR(120) NOT NULL DEFAULT 'Braunschweiger System',
  `singles_count` INT NOT NULL DEFAULT 12,
  `doubles_count` INT NOT NULL DEFAULT 4,
  `winning_sets` INT NOT NULL DEFAULT 3,
  `home_match_points` INT NOT NULL DEFAULT 0,
  `guest_match_points` INT NOT NULL DEFAULT 0,
  `is_completed` TINYINT(1) NOT NULL DEFAULT 0,
  `home_participants` JSON NULL,
  `guest_participants` JSON NULL,
  `result_details` JSON NULL,
  `players_ready` JSON NULL,
  `players_planned` JSON NULL,
  `players_played` JSON NULL,
  `status` VARCHAR(32) NOT NULL DEFAULT 'active',
  `created_by_user_id` INT NULL,
  `created_from_invitation_id` INT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `chk_friendly_match_shared_clubs_different`
    CHECK (`home_club_id` <> `guest_club_id`),
  CONSTRAINT `fk_friendly_match_shared_home_club`
    FOREIGN KEY (`home_club_id`) REFERENCES `clubs` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_friendly_match_shared_guest_club`
    FOREIGN KEY (`guest_club_id`) REFERENCES `clubs` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_friendly_match_shared_created_by`
    FOREIGN KEY (`created_by_user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_friendly_match_shared_from_invitation`
    FOREIGN KEY (`created_from_invitation_id`) REFERENCES `friendly_match_invitation` (`id`) ON DELETE SET NULL,
  KEY `idx_friendly_match_shared_home_date_time` (`home_club_id`, `date`, `start_time`),
  KEY `idx_friendly_match_shared_guest_date_time` (`guest_club_id`, `date`, `start_time`),
  KEY `idx_friendly_match_shared_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Optional rollback statements (manual use):
-- DROP TABLE IF EXISTS `friendly_match_shared`;
-- DROP TABLE IF EXISTS `friendly_match_invitation`;
