-- Flashcard System Schema for Oracle ADB
-- Run this in SQL Developer or SQLcl

-- Users table (for future SSO)
CREATE TABLE flashcard_users (
    user_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    username VARCHAR2(100) NOT NULL UNIQUE,
    email VARCHAR2(255),
    created_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    is_active CHAR(1) DEFAULT 'Y',
    CONSTRAINT flashcard_users_pk PRIMARY KEY (user_id)
);

-- Flashcard tables
CREATE TABLE flashcard_decks (
    deck_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_id NUMBER NOT NULL,
    name VARCHAR2(200) NOT NULL,
    description VARCHAR2(1000),
    language_pair VARCHAR2(20) DEFAULT 'EN-KO',
    created_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    is_active CHAR(1) DEFAULT 'Y',
    CONSTRAINT flashcard_decks_pk PRIMARY KEY (deck_id),
    CONSTRAINT flashcard_decks_fk FOREIGN KEY (user_id) REFERENCES flashcard_users(user_id)
);

CREATE TABLE flashcard_cards (
    card_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    deck_id NUMBER NOT NULL,
    question_en VARCHAR2(2000) NOT NULL,
    answer_en VARCHAR2(2000),
    question_ko VARCHAR2(2000) NOT NULL,
    answer_ko VARCHAR2(2000),
    example_sentence CLOB,
    notes CLOB,
    difficulty_level NUMBER DEFAULT 1 CHECK (difficulty_level BETWEEN 1 AND 5),
    times_reviewed NUMBER DEFAULT 0,
    times_correct NUMBER DEFAULT 0,
    last_reviewed_at TIMESTAMP,
    next_review_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    updated_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    is_active CHAR(1) DEFAULT 'Y',
    CONSTRAINT flashcard_cards_fk FOREIGN KEY (deck_id) REFERENCES flashcard_decks(deck_id)
);

-- Study sessions tracking
CREATE TABLE flashcard_sessions (
    session_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_id NUMBER NOT NULL,
    deck_id NUMBER NOT NULL,
    started_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    ended_at TIMESTAMP,
    cards_reviewed NUMBER DEFAULT 0,
    cards_correct NUMBER DEFAULT 0,
    cards_incorrect NUMBER DEFAULT 0,
    CONSTRAINT flashcard_sessions_fk1 FOREIGN KEY (user_id) REFERENCES flashcard_users(user_id),
    CONSTRAINT flashcard_sessions_fk2 FOREIGN KEY (deck_id) REFERENCES flashcard_decks(deck_id)
);

-- Study session card results
CREATE TABLE flashcard_session_results (
    result_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    session_id NUMBER NOT NULL,
    card_id NUMBER NOT NULL,
    was_correct CHAR(1) NOT NULL,
    response_time_ms NUMBER,
    attempted_at TIMESTAMP DEFAULT SYSTIMESTAMP,
    CONSTRAINT flashcard_results_fk1 FOREIGN KEY (session_id) REFERENCES flashcard_sessions(session_id),
    CONSTRAINT flashcard_results_fk2 FOREIGN KEY (card_id) REFERENCES flashcard_cards(card_id)
);

-- Indexes
CREATE INDEX flashcard_cards_idx1 ON flashcard_cards(deck_id, is_active);
CREATE INDEX flashcard_cards_idx2 ON flashcard_cards(next_review_at);
CREATE INDEX flashcard_decks_idx1 ON flashcard_decks(user_id, is_active);

-- Comments for documentation
COMMENT ON TABLE flashcard_users IS 'User accounts (for future SSO integration)';
COMMENT ON TABLE flashcard_decks IS 'Flashcard decks organized by user';
COMMENT ON TABLE flashcard_cards IS 'Individual flashcards with EN/KO translations';
COMMENT ON TABLE flashcard_sessions IS 'Study session history';
COMMENT ON TABLE flashcard_session_results IS 'Individual card results during study sessions';
