-- Idea Reviewer Schema
-- Run once to initialize the MySQL database

CREATE DATABASE IF NOT EXISTS idea_hunter
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE idea_hunter;

CREATE TABLE IF NOT EXISTS ideas (
  id                        BIGINT PRIMARY KEY AUTO_INCREMENT,
  external_idea_id          VARCHAR(64) NULL,
  raw_item_id               VARCHAR(64) NULL,
  date_found                DATETIME NULL,
  source_name               VARCHAR(100) NOT NULL,
  url                       TEXT NULL,
  raw_title                 TEXT NULL,
  target_user               TEXT NULL,
  pain_summary              TEXT NULL,
  current_workaround        TEXT NULL,
  product_idea              TEXT NULL,
  category                  VARCHAR(255) NULL,
  pain_frequency            VARCHAR(100) NULL,
  impact_type               VARCHAR(100) NULL,
  willingness_to_pay_signal DECIMAL(5,2) NULL,
  feasibility_score         DECIMAL(5,2) NULL,
  monetization_score        DECIMAL(5,2) NULL,
  distribution_score        DECIMAL(5,2) NULL,
  urgency_score             DECIMAL(5,2) NULL,
  saturation_penalty        DECIMAL(5,2) NULL,
  maintenance_risk          DECIMAL(5,2) NULL,
  personal_fit_score        DECIMAL(5,2) NULL,
  total_score               DECIMAL(6,2) NULL,
  verdict                   VARCHAR(50) NULL,
  review_status             VARCHAR(50) NOT NULL DEFAULT 'pending',
  reviewer_notes            TEXT NULL,
  reviewed_at               DATETIME NULL,
  created_at                DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at                DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  UNIQUE KEY uq_external_idea (external_idea_id),
  INDEX idx_review_status (review_status),
  INDEX idx_total_score (total_score),
  INDEX idx_created_at (created_at),
  INDEX idx_reviewed_at (reviewed_at),
  INDEX idx_status_score (review_status, total_score)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS review_actions (
  id            BIGINT PRIMARY KEY AUTO_INCREMENT,
  idea_id       BIGINT NOT NULL,
  action        VARCHAR(50) NOT NULL,
  note_snapshot TEXT NULL,
  created_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (idea_id) REFERENCES ideas(id) ON DELETE CASCADE,
  INDEX idx_idea_id (idea_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
