Spaces:
Runtime error

File size: 5,424 Bytes
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""Initial schema — all tables and indexes

Revision ID: 001
Revises:
Create Date: 2026-05-16
"""
from typing import Sequence, Union

from alembic import op

revision: str = "001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')

    op.execute("""
        CREATE TABLE IF NOT EXISTS customers (
            id            UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            name          VARCHAR(255) NOT NULL,
            phone         VARCHAR(20) UNIQUE NOT NULL,
            email         VARCHAR(255),
            account_type  VARCHAR(50) DEFAULT 'standard',
            address       TEXT,
            order_history JSONB DEFAULT '[]',
            notes         TEXT,
            created_at    TIMESTAMPTZ DEFAULT NOW(),
            updated_at    TIMESTAMPTZ DEFAULT NOW()
        )
    """)

    op.execute("""
        CREATE TABLE IF NOT EXISTS agent_profiles (
            id         UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            name       VARCHAR(255) NOT NULL,
            email      VARCHAR(255) UNIQUE NOT NULL,
            team       VARCHAR(100),
            status     VARCHAR(20) DEFAULT 'offline',
            created_at TIMESTAMPTZ DEFAULT NOW()
        )
    """)

    op.execute("""
        CREATE TABLE IF NOT EXISTS calls (
            id                UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            customer_id       UUID REFERENCES customers(id),
            agent_id          UUID REFERENCES agent_profiles(id),
            started_at        TIMESTAMPTZ DEFAULT NOW(),
            ended_at          TIMESTAMPTZ,
            duration_secs     INTEGER,
            call_type         VARCHAR(20),
            resolution        VARCHAR(20),
            escalated         BOOLEAN DEFAULT FALSE,
            escalation_reason VARCHAR(100),
            escalation_at     TIMESTAMPTZ,
            voice_ai_summary  TEXT,
            acw_summary       TEXT,
            crm_updated       BOOLEAN DEFAULT FALSE,
            status            VARCHAR(20) DEFAULT 'active'
        )
    """)

    op.execute("""
        CREATE TABLE IF NOT EXISTS transcripts (
            id        UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            call_id   UUID REFERENCES calls(id) ON DELETE CASCADE,
            speaker   VARCHAR(20),
            content   TEXT NOT NULL,
            sentiment FLOAT,
            timestamp TIMESTAMPTZ DEFAULT NOW()
        )
    """)

    op.execute("""
        CREATE TABLE IF NOT EXISTS eval_scores (
            id                   UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            call_id              UUID REFERENCES calls(id) ON DELETE CASCADE,
            agent_id             UUID REFERENCES agent_profiles(id),
            guardrail_adherence  INTEGER,
            resolution_rate      INTEGER,
            containment          INTEGER,
            caller_satisfaction  FLOAT,
            handle_time_score    INTEGER,
            disclosure_score     INTEGER,
            overall_score        INTEGER,
            pass_fail            VARCHAR(4),
            violations           JSONB DEFAULT '[]',
            coaching_note        TEXT,
            strengths            JSONB DEFAULT '[]',
            created_at           TIMESTAMPTZ DEFAULT NOW()
        )
    """)

    op.execute("""
        CREATE TABLE IF NOT EXISTS kb_documents (
            id          UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            filename    VARCHAR(255) NOT NULL,
            file_type   VARCHAR(10),
            category    VARCHAR(100) DEFAULT 'general',
            chunk_count INTEGER DEFAULT 0,
            status      VARCHAR(20) DEFAULT 'processing',
            uploaded_at TIMESTAMPTZ DEFAULT NOW()
        )
    """)

    op.execute("""
        CREATE TABLE IF NOT EXISTS coaching_packs (
            id              UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
            agent_id        UUID REFERENCES agent_profiles(id),
            generated_at    TIMESTAMPTZ DEFAULT NOW(),
            calls_analyzed  INTEGER DEFAULT 0,
            overall_trend   VARCHAR(20),
            strengths       JSONB DEFAULT '[]',
            improvements    JSONB DEFAULT '[]',
            action_items    JSONB DEFAULT '[]',
            score_summary   JSONB DEFAULT '{}'
        )
    """)

    # Indexes
    op.execute("CREATE INDEX IF NOT EXISTS idx_calls_customer ON calls(customer_id)")
    op.execute("CREATE INDEX IF NOT EXISTS idx_calls_agent ON calls(agent_id)")
    op.execute("CREATE INDEX IF NOT EXISTS idx_calls_status ON calls(status)")
    op.execute("CREATE INDEX IF NOT EXISTS idx_transcripts_call ON transcripts(call_id)")
    op.execute("CREATE INDEX IF NOT EXISTS idx_eval_scores_call ON eval_scores(call_id)")
    op.execute("CREATE INDEX IF NOT EXISTS idx_eval_scores_agent ON eval_scores(agent_id)")


def downgrade() -> None:
    op.execute("DROP TABLE IF EXISTS coaching_packs CASCADE")
    op.execute("DROP TABLE IF EXISTS kb_documents CASCADE")
    op.execute("DROP TABLE IF EXISTS eval_scores CASCADE")
    op.execute("DROP TABLE IF EXISTS transcripts CASCADE")
    op.execute("DROP TABLE IF EXISTS calls CASCADE")
    op.execute("DROP TABLE IF EXISTS agent_profiles CASCADE")
    op.execute("DROP TABLE IF EXISTS customers CASCADE")