-- Ensenada Run Database Schema
-- Database: ensenadarun

CREATE DATABASE IF NOT EXISTS ensenadarun;
USE ensenadarun;

-- Table: role
-- Stores user roles for permissions
CREATE TABLE IF NOT EXISTS `role` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(100) NOT NULL UNIQUE,
  INDEX idx_name (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: role_user
-- Many-to-many relationship between users and roles
CREATE TABLE IF NOT EXISTS `role_user` (
  `user_id` INT NOT NULL,
  `role_id` INT NOT NULL,
  PRIMARY KEY (`user_id`, `role_id`),
  FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`role_id`) REFERENCES `role`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: image
-- Stores uploaded images
CREATE TABLE IF NOT EXISTS `image` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `filename` VARCHAR(255) NOT NULL,
  `original_filename` VARCHAR(255) NOT NULL,
  `path` VARCHAR(500) NOT NULL,
  `content_type` VARCHAR(100) NOT NULL,
  `size` INT NOT NULL,
  `width` INT NOT NULL,
  `height` INT NOT NULL,
  `user_id` INT NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_user_id (`user_id`),
  FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: attachment
-- Stores uploaded file attachments
CREATE TABLE IF NOT EXISTS `attachment` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `filename` VARCHAR(255) NOT NULL,
  `original_filename` VARCHAR(255) NOT NULL,
  `path` VARCHAR(500) NOT NULL,
  `content_type` VARCHAR(100) NOT NULL,
  `size` INT NOT NULL,
  `extension` VARCHAR(10) NOT NULL,
  `user_id` INT NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_user_id (`user_id`),
  FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: race
-- Stores athletic race information
CREATE TABLE IF NOT EXISTS `race` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(255) NOT NULL,
  `city` VARCHAR(100) NOT NULL,
  `main_distance` VARCHAR(50) NOT NULL,
  `poster_image_id` INT NOT NULL,
  `price` DECIMAL(10,2) NOT NULL DEFAULT 0,
  `page_link` VARCHAR(500),
  `registration_link` VARCHAR(500),
  `type` VARCHAR(100) NOT NULL,
  `datetime_start` DATETIME NOT NULL,
  `description` TEXT,
  `is_popular` BOOLEAN DEFAULT FALSE,
  `accepted` BOOLEAN NOT NULL DEFAULT 0,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` DATETIME NULL DEFAULT NULL,
  INDEX idx_datetime_start (`datetime_start`),
  INDEX idx_deleted (`deleted`),
  INDEX idx_city (`city`),
  INDEX idx_type (`type`),
  INDEX idx_is_popular (`is_popular`),
  INDEX idx_accepted (`accepted`),
  FOREIGN KEY (`poster_image_id`) REFERENCES `image`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: gallery_link
-- Stores external photo gallery links for races
CREATE TABLE IF NOT EXISTS `gallery_link` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `race_id` INT NOT NULL,
  `url` VARCHAR(500) NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` DATETIME NULL DEFAULT NULL,
  INDEX idx_race_id (`race_id`),
  INDEX idx_deleted (`deleted`),
  FOREIGN KEY (`race_id`) REFERENCES `race`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Insert default roles
INSERT INTO `role` (`name`) VALUES
  ('admin'),
  ('user')
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- Show table structure
SHOW TABLES;
