-- sso_users에 입사일 추가
ALTER TABLE sso_users ADD COLUMN IF NOT EXISTS hire_date DATE NULL COMMENT '입사일';

-- 전사 휴가 신청
CREATE TABLE IF NOT EXISTS sp_vacation_requests (
  request_id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL COMMENT 'sso_users.user_id',
  vacation_type_id INT UNSIGNED NOT NULL,
  start_date DATE NOT NULL,
  end_date DATE NOT NULL,
  days_used DECIMAL(4,1) NOT NULL,
  reason TEXT,
  status ENUM('pending','approved','rejected','cancelled') DEFAULT 'pending',
  reviewed_by INT NULL,
  reviewed_at TIMESTAMP NULL,
  review_note TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES sso_users(user_id),
  FOREIGN KEY (reviewed_by) REFERENCES sso_users(user_id),
  FOREIGN KEY (vacation_type_id) REFERENCES vacation_types(id),
  INDEX idx_user_status (user_id, status),
  INDEX idx_dates (start_date, end_date)
);

-- 전사 휴가 잔여일
CREATE TABLE IF NOT EXISTS sp_vacation_balances (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL COMMENT 'sso_users.user_id',
  vacation_type_id INT UNSIGNED NOT NULL,
  year INT NOT NULL,
  total_days DECIMAL(4,1) DEFAULT 0,
  used_days DECIMAL(4,1) DEFAULT 0,
  notes TEXT,
  created_by INT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY unique_user_type_year (user_id, vacation_type_id, year),
  FOREIGN KEY (user_id) REFERENCES sso_users(user_id),
  FOREIGN KEY (created_by) REFERENCES sso_users(user_id),
  FOREIGN KEY (vacation_type_id) REFERENCES vacation_types(id)
);
