-- 부서별 페이지 권한 관리 기능 DB 마이그레이션
-- NAS SSH 접속 후 MariaDB 컨테이너에서 실행

-- 1) sso_users에 department_id FK 추가
ALTER TABLE sso_users
  ADD COLUMN department_id INT NULL AFTER department,
  ADD CONSTRAINT fk_sso_users_dept FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE SET NULL;

-- 2) department_page_permissions 테이블 생성
CREATE TABLE department_page_permissions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  department_id INT NOT NULL,
  page_name VARCHAR(50) NOT NULL,
  can_access BOOLEAN DEFAULT FALSE,
  granted_by_id INT NULL,
  granted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY unique_dept_page (department_id, page_name),
  FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE CASCADE
);
