-- 创建数据库
CREATE DATABASE IF NOT EXISTS quanyoumi CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE quanyoumi;

-- 活动表
CREATE TABLE IF NOT EXISTS Activities (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  location VARCHAR(255),
  locationLat DECIMAL(10, 7),
  locationLng DECIMAL(10, 7),
  startTime DATETIME,
  endTime DATETIME,
  registrationDeadline DATETIME,
  maxParticipants INT,
  budget DECIMAL(10, 2) DEFAULT 0 COMMENT '活动预算',
  status ENUM('upcoming', 'ongoing', 'completed') DEFAULT 'upcoming',
  coverImage VARCHAR(255),
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- 报名表
CREATE TABLE IF NOT EXISTS Registrations (
  id INT AUTO_INCREMENT PRIMARY KEY,
  activityId INT NOT NULL,
  name VARCHAR(100) NOT NULL,
  phone VARCHAR(20) NOT NULL,
  carModel VARCHAR(100),
  carNumber VARCHAR(20),
  peopleCount INT DEFAULT 1,
  status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
  note TEXT,
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (activityId) REFERENCES Activities(id) ON DELETE CASCADE
);

-- 经费表
CREATE TABLE IF NOT EXISTS Expenses (
  id INT AUTO_INCREMENT PRIMARY KEY,
  activityId INT NOT NULL,
  item VARCHAR(255) NOT NULL,
  amount DECIMAL(10, 2) NOT NULL,
  category VARCHAR(100),
  receipt VARCHAR(255),
  note TEXT,
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (activityId) REFERENCES Activities(id) ON DELETE CASCADE
);

-- 照片表
CREATE TABLE IF NOT EXISTS Photos (
  id INT AUTO_INCREMENT PRIMARY KEY,
  activityId INT NOT NULL,
  url VARCHAR(255) NOT NULL,
  type ENUM('image', 'video') DEFAULT 'image',
  description VARCHAR(255),
  uploadedBy VARCHAR(100),
  status ENUM('pending', 'approved', 'rejected') DEFAULT 'approved',
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (activityId) REFERENCES Activities(id) ON DELETE CASCADE
);

-- 赞助商表
CREATE TABLE IF NOT EXISTS Sponsors (
  id INT AUTO_INCREMENT PRIMARY KEY,
  activityId INT NOT NULL,
  name VARCHAR(255) NOT NULL,
  logo VARCHAR(255),
  type ENUM('title', 'sponsor') DEFAULT 'sponsor',
  link VARCHAR(255),
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (activityId) REFERENCES Activities(id) ON DELETE CASCADE
);

-- 管理员表
CREATE TABLE IF NOT EXISTS Admins (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  name VARCHAR(100),
  createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
