-- 알림 시스템 테이블 생성
-- 실행: docker exec -i tkfb_db mysql -u hyungi -p'your_password' hyungi < db/migrations/20260205004000_create_notifications.sql

-- ============================================
-- STEP 1: notifications 테이블 생성
-- ============================================

CREATE TABLE IF NOT EXISTS notifications (
  notification_id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NULL COMMENT '특정 사용자에게만 표시 (NULL이면 전체)',
  type ENUM('repair', 'safety', 'system', 'equipment', 'maintenance') NOT NULL DEFAULT 'system',
  title VARCHAR(200) NOT NULL,
  message TEXT,
  link_url VARCHAR(500) COMMENT '클릭시 이동할 URL',
  reference_type VARCHAR(50) COMMENT '연관 테이블 (equipment_repair_requests 등)',
  reference_id INT COMMENT '연관 레코드 ID',
  is_read BOOLEAN DEFAULT FALSE,
  read_at DATETIME NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  created_by INT NULL,
  INDEX idx_notifications_user (user_id),
  INDEX idx_notifications_type (type),
  INDEX idx_notifications_is_read (is_read),
  INDEX idx_notifications_created (created_at DESC)
);

-- 수리 요청 테이블 수정 (알림 연동을 위해)
-- equipment_repair_requests 테이블이 없으면 생성
CREATE TABLE IF NOT EXISTS equipment_repair_requests (
  request_id INT AUTO_INCREMENT PRIMARY KEY,
  equipment_id INT UNSIGNED NOT NULL,
  repair_type VARCHAR(100) NOT NULL,
  description TEXT,
  urgency ENUM('low', 'normal', 'high', 'urgent') DEFAULT 'normal',
  status ENUM('pending', 'in_progress', 'completed', 'cancelled') DEFAULT 'pending',
  requested_by INT,
  assigned_to INT NULL,
  completed_at DATETIME NULL,
  notes TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (equipment_id) REFERENCES equipments(equipment_id) ON DELETE CASCADE,
  INDEX idx_err_equipment (equipment_id),
  INDEX idx_err_status (status),
  INDEX idx_err_created (created_at DESC)
);
