-- Questions table for structured architect questions
CREATE TABLE IF NOT EXISTS questions (
    id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    conversation_id UUID REFERENCES conversations(id) ON DELETE CASCADE,
    project_id      TEXT NOT NULL,
    question_type   VARCHAR(20) NOT NULL,  -- 'text', 'choice', 'multichoice', 'yesno'
    text            TEXT NOT NULL,
    choices         TEXT[],                 -- For choice/multichoice types
    answer          TEXT,                   -- User's text answer
    answer_choices  TEXT[],                 -- For multichoice answers
    metadata        JSONB DEFAULT '{}',
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    answered_at     TIMESTAMPTZ
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_questions_conversation
    ON questions(conversation_id, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_questions_project
    ON questions(project_id, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_questions_unanswered
    ON questions(project_id, answered_at) WHERE answered_at IS NULL;

COMMENT ON TABLE questions IS 'Structured questions for architect service';
COMMENT ON COLUMN questions.question_type IS 'Question type: text, choice, multichoice, yesno';
COMMENT ON COLUMN questions.choices IS 'Available choices for choice/multichoice questions';
COMMENT ON COLUMN questions.answer_choices IS 'Selected choices for multichoice answers';
