-- Combined schema for the event management system

-- Drop existing tables to ensure a clean slate.
-- This is destructive and should not be used in production with existing data.
DROP TABLE IF EXISTS `event_vendors`, `event_budgets`, `event_guests`, `event_schedules`, `events`, `vendors`, `venues`, `users`, `roles`;

-- Table structure for table `roles`
CREATE TABLE `roles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL UNIQUE,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed default roles
INSERT INTO `roles` (`name`) VALUES ('Super-Admin'), ('Event Organizer'), ('Vendor'), ('Guest');

-- Table structure for table `users`
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role_id` int(11) NOT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `venues`
CREATE TABLE `venues` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(255) NOT NULL,
    `capacity` INT,
    `amenities` TEXT,
    `availability` BOOLEAN DEFAULT TRUE,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `vendors`
CREATE TABLE `vendors` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(255) NOT NULL,
    `service_type` VARCHAR(255),
    `contact_info` VARCHAR(255),
    `rating` DECIMAL(3, 2),
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `events`
CREATE TABLE `events` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `organizer_id` INT NOT NULL,
    `venue_id` INT,
    `name` VARCHAR(255) NOT NULL,
    `description` TEXT,
    `start_time` DATETIME,
    `end_time` DATETIME,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`organizer_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`venue_id`) REFERENCES `venues`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `event_schedules`
CREATE TABLE `event_schedules` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `event_id` INT NOT NULL,
    `activity` VARCHAR(255) NOT NULL,
    `start_time` DATETIME NOT NULL,
    `end_time` DATETIME NOT NULL,
    FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `event_guests`
CREATE TABLE `event_guests` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `event_id` INT NOT NULL,
    `user_id` INT,
    `name` VARCHAR(255) NOT NULL,
    `email` VARCHAR(255),
    `rsvp_status` ENUM('attending', 'not_attending', 'maybe') NOT NULL,
    `meal_preference` VARCHAR(255),
    FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `event_budgets`
CREATE TABLE `event_budgets` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `event_id` INT NOT NULL,
    `item_name` VARCHAR(255) NOT NULL,
    `estimated_cost` DECIMAL(10, 2) NOT NULL,
    `actual_cost` DECIMAL(10, 2),
    `paid_status` BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Table structure for table `event_vendors`
CREATE TABLE `event_vendors` (
    `event_id` INT NOT NULL,
    `vendor_id` INT NOT NULL,
    PRIMARY KEY (`event_id`, `vendor_id`),
    FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`vendor_id`) REFERENCES `vendors`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;