-- Core conversation flow definitions
CREATE TABLE conversation_flows (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(100) NOT NULL,
    description TEXT,
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Define individual slots with their properties
CREATE TABLE slot_definitions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    flow_id UUID REFERENCES conversation_flows(id),
    slot_name VARCHAR(100) NOT NULL,
    slot_type VARCHAR(50) NOT NULL, -- 'text', 'number', 'email', 'phone', 'date', 'boolean', 'choice'
    is_required BOOLEAN DEFAULT true,
    priority INTEGER DEFAULT 1, -- Lower numbers = higher priority
    depends_on_slot VARCHAR(100), -- Optional dependency on another slot
    dependency_value JSONB, -- Required value(s) for dependency
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    UNIQUE(flow_id, slot_name)
);

-- Validation rules for each slot
CREATE TABLE slot_validations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    slot_id UUID REFERENCES slot_definitions(id),
    validation_type VARCHAR(50) NOT NULL, -- 'regex', 'range', 'length', 'choices', 'custom'
    validation_rule JSONB NOT NULL, -- Store validation parameters
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Question templates for each slot
CREATE TABLE slot_questions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    slot_id UUID REFERENCES slot_definitions(id),
    question_type VARCHAR(50) NOT NULL, -- 'initial', 'clarification', 'validation_error', 'confirmation'
    question_text TEXT NOT NULL,
    follow_up_text TEXT, -- Optional follow-up or explanation
    is_default BOOLEAN DEFAULT false,
    context_conditions JSONB, -- When to use this question variant
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Store conversation state and collected slot values
CREATE TABLE conversation_sessions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id VARCHAR(100) NOT NULL,
    flow_id UUID REFERENCES conversation_flows(id),
    session_status VARCHAR(50) DEFAULT 'active', -- 'active', 'completed', 'paused', 'abandoned'
    current_slot VARCHAR(100),
    collected_slots JSONB DEFAULT '{}',
    context_data JSONB DEFAULT '{}', -- Additional context
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    last_interaction TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Track individual slot collection attempts
CREATE TABLE slot_collection_attempts (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    session_id UUID REFERENCES conversation_sessions(id),
    slot_name VARCHAR(100) NOT NULL,
    user_input TEXT,
    extracted_value JSONB,
    is_valid BOOLEAN,
    validation_errors JSONB,
    attempt_number INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Sample data for a customer onboarding flow
INSERT INTO conversation_flows (name, description) VALUES 
('customer_onboarding', 'New customer information collection');

-- Sample slot definitions
INSERT INTO slot_definitions (flow_id, slot_name, slot_type, is_required, priority) 
SELECT id, 'full_name', 'text', true, 1 FROM conversation_flows WHERE name = 'customer_onboarding';

INSERT INTO slot_definitions (flow_id, slot_name, slot_type, is_required, priority) 
SELECT id, 'email', 'email', true, 2 FROM conversation_flows WHERE name = 'customer_onboarding';

INSERT INTO slot_definitions (flow_id, slot_name, slot_type, is_required, priority, depends_on_slot, dependency_value) 
SELECT id, 'phone', 'phone', true, 3, 'contact_preference', '["phone", "both"]' 
FROM conversation_flows WHERE name = 'customer_onboarding';

-- Sample validation rules
INSERT INTO slot_validations (slot_id, validation_type, validation_rule, error_message)
SELECT id, 'length', '{"min": 2, "max": 100}', 'Name must be between 2 and 100 characters'
FROM slot_definitions WHERE slot_name = 'full_name';

INSERT INTO slot_validations (slot_id, validation_type, validation_rule, error_message)
SELECT id, 'regex', '{"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"}', 'Please enter a valid email address'
FROM slot_definitions WHERE slot_name = 'email';

-- Sample questions
INSERT INTO slot_questions (slot_id, question_type, question_text, follow_up_text)
SELECT id, 'initial', 'Hi! I''d like to help you get started. What''s your full name?', 'Please provide your first and last name'
FROM slot_definitions WHERE slot_name = 'full_name';

INSERT INTO slot_questions (slot_id, question_type, question_text)
SELECT id, 'validation_error', 'That doesn''t look like a valid name. Could you please try again?'
FROM slot_definitions WHERE slot_name = 'full_name';

-- Indexes for performance
CREATE INDEX idx_conversation_sessions_user_flow ON conversation_sessions(user_id, flow_id);
CREATE INDEX idx_conversation_sessions_status ON conversation_sessions(session_status);
CREATE INDEX idx_slot_definitions_flow_priority ON slot_definitions(flow_id, priority);
CREATE INDEX idx_slot_collection_attempts_session ON slot_collection_attempts(session_id);
