-- Training Videos Table
CREATE TABLE IF NOT EXISTS training_videos (
  id VARCHAR(36) PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  video_url VARCHAR(500) NOT NULL,
  thumbnail_url VARCHAR(500),
  module VARCHAR(100) NOT NULL,
  duration INT DEFAULT 300,
  file_key VARCHAR(255),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  created_by VARCHAR(36),
  is_active BOOLEAN DEFAULT true,
  INDEX idx_module (module),
  INDEX idx_created_at (created_at)
);

-- Quiz Questions Table
CREATE TABLE IF NOT EXISTS quiz_questions (
  id VARCHAR(36) PRIMARY KEY,
  text TEXT NOT NULL,
  type ENUM('multiple-choice', 'true-false', 'short-answer') NOT NULL,
  module VARCHAR(100) NOT NULL,
  options JSON,
  correct_answer VARCHAR(500) NOT NULL,
  points INT DEFAULT 10,
  explanation TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  created_by VARCHAR(36),
  is_active BOOLEAN DEFAULT true,
  INDEX idx_module (module),
  INDEX idx_type (type)
);

-- Admin Certifications Table
CREATE TABLE IF NOT EXISTS admin_certifications (
  id VARCHAR(36) PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  level ENUM('basic', 'intermediate', 'advanced') NOT NULL,
  required_modules JSON,
  passing_score INT DEFAULT 70,
  expiration_days INT DEFAULT 365,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  created_by VARCHAR(36),
  is_active BOOLEAN DEFAULT true,
  INDEX idx_level (level)
);

-- Certification Issues Table
CREATE TABLE IF NOT EXISTS certification_issues (
  id VARCHAR(36) PRIMARY KEY,
  certification_id VARCHAR(36) NOT NULL,
  admin_id VARCHAR(36) NOT NULL,
  admin_name VARCHAR(255) NOT NULL,
  admin_email VARCHAR(255),
  certificate_number VARCHAR(100) UNIQUE,
  issue_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  expiration_date TIMESTAMP,
  verification_url VARCHAR(500),
  is_revoked BOOLEAN DEFAULT false,
  revoked_at TIMESTAMP,
  revoked_reason TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (certification_id) REFERENCES admin_certifications(id),
  INDEX idx_admin_id (admin_id),
  INDEX idx_certification_id (certification_id),
  INDEX idx_certificate_number (certificate_number),
  INDEX idx_issue_date (issue_date)
);

-- Admin Training Progress Table
CREATE TABLE IF NOT EXISTS admin_training_progress (
  id VARCHAR(36) PRIMARY KEY,
  admin_id VARCHAR(36) NOT NULL,
  module VARCHAR(100) NOT NULL,
  video_id VARCHAR(36),
  question_id VARCHAR(36),
  certification_id VARCHAR(36),
  progress_type ENUM('video-watched', 'question-answered', 'quiz-completed', 'certification-earned') NOT NULL,
  score INT,
  completed_at TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (video_id) REFERENCES training_videos(id),
  FOREIGN KEY (question_id) REFERENCES quiz_questions(id),
  FOREIGN KEY (certification_id) REFERENCES admin_certifications(id),
  INDEX idx_admin_id (admin_id),
  INDEX idx_module (module),
  INDEX idx_progress_type (progress_type),
  INDEX idx_completed_at (completed_at)
);

-- Admin Badges Table
CREATE TABLE IF NOT EXISTS admin_badges (
  id VARCHAR(36) PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  icon_url VARCHAR(500),
  badge_type ENUM('training', 'achievement', 'milestone') NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_badge_type (badge_type)
);

-- Admin Badge Awards Table
CREATE TABLE IF NOT EXISTS admin_badge_awards (
  id VARCHAR(36) PRIMARY KEY,
  admin_id VARCHAR(36) NOT NULL,
  badge_id VARCHAR(36) NOT NULL,
  awarded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (badge_id) REFERENCES admin_badges(id),
  INDEX idx_admin_id (admin_id),
  INDEX idx_badge_id (badge_id),
  INDEX idx_awarded_at (awarded_at)
);

-- Quiz Attempts Table
CREATE TABLE IF NOT EXISTS quiz_attempts (
  id VARCHAR(36) PRIMARY KEY,
  admin_id VARCHAR(36) NOT NULL,
  question_id VARCHAR(36) NOT NULL,
  module VARCHAR(100) NOT NULL,
  answer VARCHAR(500),
  is_correct BOOLEAN,
  score INT,
  attempted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (question_id) REFERENCES quiz_questions(id),
  INDEX idx_admin_id (admin_id),
  INDEX idx_question_id (question_id),
  INDEX idx_module (module),
  INDEX idx_attempted_at (attempted_at)
);

-- Training Analytics Table
CREATE TABLE IF NOT EXISTS training_analytics (
  id VARCHAR(36) PRIMARY KEY,
  admin_id VARCHAR(36) NOT NULL,
  module VARCHAR(100) NOT NULL,
  videos_watched INT DEFAULT 0,
  questions_answered INT DEFAULT 0,
  quiz_score DECIMAL(5,2),
  certification_earned BOOLEAN DEFAULT false,
  total_time_spent INT DEFAULT 0,
  last_activity TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_admin_id (admin_id),
  INDEX idx_module (module)
);

-- Create indexes for better query performance
CREATE INDEX idx_training_videos_module_active ON training_videos(module, is_active);
CREATE INDEX idx_quiz_questions_module_active ON quiz_questions(module, is_active);
CREATE INDEX idx_certification_issues_admin_active ON certification_issues(admin_id, is_revoked);
CREATE INDEX idx_admin_training_progress_admin_module ON admin_training_progress(admin_id, module);
