-- 볼링 매니저 데이터베이스 스키마
-- 데이터베이스 생성
CREATE DATABASE IF NOT EXISTS bowlingManager;
USE bowlingManager;

-- 사용자 테이블
CREATE TABLE IF NOT EXISTS Users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(255) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL UNIQUE,
  role ENUM('admin', 'clubadmin', 'user') NOT NULL DEFAULT 'user',
  status ENUM('active', 'inactive', 'suspended') NOT NULL DEFAULT 'active',
  lastLoginAt DATETIME,
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL
);

-- 클럽 테이블
CREATE TABLE IF NOT EXISTS Clubs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  ownerId INT NOT NULL,
  location VARCHAR(255),
  contact VARCHAR(255),
  status ENUM('active', 'inactive', 'suspended') NOT NULL DEFAULT 'active',
  memberCount INT NOT NULL DEFAULT 0,
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (ownerId) REFERENCES Users(id)
);

-- 기능 테이블
CREATE TABLE IF NOT EXISTS Features (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  code VARCHAR(255) NOT NULL UNIQUE,
  price INT NOT NULL DEFAULT 0,
  status ENUM('active', 'inactive', 'suspended') NOT NULL DEFAULT 'active',
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL
);

-- 구독 플랜 테이블
CREATE TABLE IF NOT EXISTS SubscriptionPlans (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  maxMembers INT NOT NULL,
  price INT NOT NULL DEFAULT 0,
  status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL
);

-- 클럽 구독 테이블
CREATE TABLE IF NOT EXISTS ClubSubscriptions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  clubId INT NOT NULL,
  planId INT NOT NULL,
  status ENUM('active', 'expired', 'suspended') NOT NULL DEFAULT 'active',
  startDate DATETIME NOT NULL,
  endDate DATETIME NOT NULL,
  price INT NOT NULL DEFAULT 0,
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (clubId) REFERENCES Clubs(id),
  FOREIGN KEY (planId) REFERENCES SubscriptionPlans(id)
);

-- 구독 플랜 기능 테이블
CREATE TABLE IF NOT EXISTS SubscriptionPlanFeatures (
  id INT AUTO_INCREMENT PRIMARY KEY,
  planId INT NOT NULL,
  featureId INT NOT NULL,
  status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (planId) REFERENCES SubscriptionPlans(id),
  FOREIGN KEY (featureId) REFERENCES Features(id)
);

-- 클럽 기능 테이블 (N:M 관계)
CREATE TABLE IF NOT EXISTS ClubFeatures (
  id INT AUTO_INCREMENT PRIMARY KEY,
  clubId INT NOT NULL,
  featureId INT NOT NULL,
  enabled BOOLEAN NOT NULL DEFAULT TRUE,
  expiryDate DATETIME,
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (clubId) REFERENCES Clubs(id),
  FOREIGN KEY (featureId) REFERENCES Features(id)
);

-- 회원 테이블
CREATE TABLE IF NOT EXISTS Members (
  id INT AUTO_INCREMENT PRIMARY KEY,
  clubId INT NOT NULL,
  userId INT,
  name VARCHAR(255) NOT NULL,
  gender ENUM('남성', '여성') NOT NULL,
  memberType ENUM('정회원', '준회원', '운영진', '모임장') NOT NULL DEFAULT '준회원',
  handicap INT NOT NULL DEFAULT 0,
  average FLOAT NOT NULL DEFAULT 0,
  games INT NOT NULL DEFAULT 0,
  phone VARCHAR(255),
  email VARCHAR(255),
  joinDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status ENUM('active', 'inactive', 'suspended') NOT NULL DEFAULT 'active',
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (clubId) REFERENCES Clubs(id),
  FOREIGN KEY (userId) REFERENCES Users(id)
);

-- 이벤트 테이블
CREATE TABLE IF NOT EXISTS `Events` (
  `id` int NOT NULL AUTO_INCREMENT,
  `clubId` int NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text,
  `eventType` enum('정기전', '번개', '연습', '교류전', '이벤트', '기타') NOT NULL DEFAULT '정기전',
  `startDate` datetime NOT NULL,
  `endDate` datetime,
  `location` varchar(255),
  `gameCount` int NOT NULL DEFAULT 3,
  `entryFee` int NOT NULL DEFAULT 0,
  `maxParticipants` int NOT NULL DEFAULT 0,
  `registrationDeadline` datetime,
  `status` enum('active', 'inactive', 'deleted') NOT NULL DEFAULT 'active',
  `createdAt` datetime NOT NULL,
  `updatedAt` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `clubId` (`clubId`),
  CONSTRAINT `events_ibfk_1` FOREIGN KEY (`clubId`) REFERENCES `Clubs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- 이벤트 참가자 테이블
CREATE TABLE IF NOT EXISTS EventParticipants (
  id INT AUTO_INCREMENT PRIMARY KEY,
  eventId INT NOT NULL,
  memberId INT NOT NULL,
  status ENUM('참가예정', '참가완료', '취소') NOT NULL DEFAULT '참가예정',
  paymentStatus ENUM('미납', '납부완료') NOT NULL DEFAULT '미납',
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (eventId) REFERENCES Events(id),
  FOREIGN KEY (memberId) REFERENCES Members(id)
);

-- 이벤트 점수 테이블
CREATE TABLE IF NOT EXISTS EventScores (
  id INT AUTO_INCREMENT PRIMARY KEY,
  eventId INT NOT NULL,
  participantId INT NOT NULL,
  gameNumber INT NOT NULL,
  score INT NOT NULL,
  handicap INT NOT NULL DEFAULT 0,
  totalScore INT NOT NULL,
  strikes INT NOT NULL DEFAULT 0,
  spares INT NOT NULL DEFAULT 0,
  openFrames INT NOT NULL DEFAULT 0,
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL,
  FOREIGN KEY (eventId) REFERENCES Events(id),
  FOREIGN KEY (participantId) REFERENCES EventParticipants(id)
);

-- 메뉴 테이블
CREATE TABLE IF NOT EXISTS menus (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  icon VARCHAR(255),
  `to` VARCHAR(255) NOT NULL,
  role VARCHAR(255) NOT NULL DEFAULT 'user',
  `order` INT NOT NULL DEFAULT 0,
  visible BOOLEAN NOT NULL DEFAULT TRUE,
  createdAt DATETIME NOT NULL,
  updatedAt DATETIME NOT NULL
);

-- 알림 테이블
CREATE TABLE IF NOT EXISTS Notifications (
  id INT AUTO_INCREMENT PRIMARY KEY,
  userId INT NOT NULL,
  title VARCHAR(255) NOT NULL,
  message TEXT NOT NULL,
  icon VARCHAR(255) DEFAULT 'pi pi-info-circle',
  type ENUM('info', 'success', 'warning', 'error') NOT NULL DEFAULT 'info',
  read BOOLEAN NOT NULL DEFAULT FALSE,
  link VARCHAR(255) DEFAULT NULL,
  createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (userId) REFERENCES Users(id) ON DELETE CASCADE
);

-- 관리자 활동 로그 테이블
CREATE TABLE IF NOT EXISTS ActivityLogs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  userId INT NOT NULL,
  type VARCHAR(50) NOT NULL,
  description TEXT,
  createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (userId) REFERENCES Users(id) ON DELETE CASCADE
);
