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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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
Synthetic customer support ticket dataset.
Provides realistic, diverse tickets across multiple categories.
"""
from __future__ import annotations

from typing import Any, Dict, List
from datetime import datetime, timezone, timedelta
import random

# ─── Ticket Templates ─────────────────────────────────────────────────────────

TICKET_TEMPLATES: List[Dict[str, Any]] = [

    # ── BILLING tickets ──────────────────────────────────────────────────────
    {
        "id": "T001",
        "subject": "Double charged for my last order",
        "body": (
            "Hi, I noticed that I was charged twice for order #ORD-88234. "
            "My bank statement shows two identical charges of $89.99 on November 14th. "
            "I only placed one order. Please refund the duplicate charge ASAP. "
            "This is very frustrating."
        ),
        "true_category": "billing",
        "true_priority": "high",
        "true_tags": ["duplicate-charge", "refund", "urgent"],
        "customer": {
            "customer_id": "C1001",
            "name": "Sarah Johnson",
            "email": "sarah.j@example.com",
            "account_tier": "standard",
            "total_orders": 12,
            "previous_tickets": 1,
            "sentiment": "angry",
        },
        "ideal_resolution": "refunded",
        "sla_hours": 4,
        "escalation_needed": False,
        "complexity": "easy",
    },
    {
        "id": "T002",
        "subject": "Subscription renewal charge I didn't authorize",
        "body": (
            "I cancelled my Premium subscription 3 weeks ago but you just charged me $199 again. "
            "I have a confirmation email of the cancellation (ref: CAN-20341). "
            "I demand a full refund and proof that my subscription is actually cancelled. "
            "If this is not resolved I will dispute the charge with my bank."
        ),
        "true_category": "billing",
        "true_priority": "urgent",
        "true_tags": ["unauthorized-charge", "subscription", "refund", "chargeback-risk"],
        "customer": {
            "customer_id": "C1002",
            "name": "Marcus Chen",
            "email": "marcus.chen@example.com",
            "account_tier": "premium",
            "total_orders": 45,
            "previous_tickets": 3,
            "sentiment": "angry",
        },
        "ideal_resolution": "refunded",
        "sla_hours": 2,
        "escalation_needed": True,
        "escalation_team": "billing_specialist",
        "complexity": "medium",
    },

    # ── TECHNICAL tickets ────────────────────────────────────────────────────
    {
        "id": "T003",
        "subject": "App crashes when I try to upload photos",
        "body": (
            "Every time I try to upload photos from my iPhone 14 Pro, the app crashes immediately. "
            "I'm on iOS 17.2 and app version 4.3.1. I've tried restarting, reinstalling, and clearing cache. "
            "Nothing works. This has been happening for 3 days. "
            "I need to upload photos for my business listings urgently."
        ),
        "true_category": "technical",
        "true_priority": "high",
        "true_tags": ["ios", "crash", "upload", "bug"],
        "customer": {
            "customer_id": "C1003",
            "name": "Priya Patel",
            "email": "priya.patel@mybusiness.com",
            "account_tier": "premium",
            "total_orders": 8,
            "previous_tickets": 2,
            "sentiment": "negative",
        },
        "ideal_resolution": "workaround",
        "sla_hours": 8,
        "escalation_needed": True,
        "escalation_team": "engineering",
        "complexity": "medium",
    },
    {
        "id": "T004",
        "subject": "Can't log in - password reset not working",
        "body": (
            "I forgot my password and tried to reset it. I get the reset email but when I click the link "
            "it says 'link expired' even when I click it immediately. "
            "I've tried 5 times in the last hour. Please help."
        ),
        "true_category": "technical",
        "true_priority": "medium",
        "true_tags": ["login", "password-reset", "authentication"],
        "customer": {
            "customer_id": "C1004",
            "name": "Tom Williams",
            "email": "tomw@personalmail.com",
            "account_tier": "standard",
            "total_orders": 3,
            "previous_tickets": 0,
            "sentiment": "neutral",
        },
        "ideal_resolution": "fixed",
        "sla_hours": 8,
        "escalation_needed": False,
        "complexity": "easy",
    },

    # ── SHIPPING tickets ─────────────────────────────────────────────────────
    {
        "id": "T005",
        "subject": "Package marked delivered but not received",
        "body": (
            "My tracking number TRK-4892751 shows 'delivered' since yesterday at 2pm but I never received my package. "
            "I was home all day. My neighbor hasn't seen it either. "
            "The order value is $340. I'm very worried it was stolen. "
            "What can you do about this?"
        ),
        "true_category": "shipping",
        "true_priority": "high",
        "true_tags": ["missing-package", "delivered-not-received", "high-value"],
        "customer": {
            "customer_id": "C1005",
            "name": "Emily Rodriguez",
            "email": "emily.r@emaildomain.com",
            "account_tier": "standard",
            "total_orders": 7,
            "previous_tickets": 0,
            "sentiment": "negative",
        },
        "ideal_resolution": "refunded",
        "sla_hours": 24,
        "escalation_needed": False,
        "complexity": "medium",
    },
    {
        "id": "T006",
        "subject": "Order arriving 3 weeks late - wedding gift",
        "body": (
            "I ordered a gift for my sister's wedding on Oct 15 with guaranteed 5-day delivery. "
            "Today is Nov 5 and the item still hasn't arrived (order #ORD-77291). "
            "The wedding was last Saturday! This gift was supposed to be special. "
            "I need either overnight shipping or a full refund. This is completely unacceptable."
        ),
        "true_category": "shipping",
        "true_priority": "urgent",
        "true_tags": ["late-delivery", "refund", "compensation"],
        "customer": {
            "customer_id": "C1006",
            "name": "James Park",
            "email": "james.park@outlook.com",
            "account_tier": "standard",
            "total_orders": 4,
            "previous_tickets": 1,
            "sentiment": "angry",
        },
        "ideal_resolution": "refunded",
        "sla_hours": 4,
        "escalation_needed": False,
        "complexity": "easy",
    },

    # ── RETURNS tickets ──────────────────────────────────────────────────────
    {
        "id": "T007",
        "subject": "Defective product - need return instructions",
        "body": (
            "I received the Bluetooth speaker (SKU: BT-X200) but the right speaker channel doesn't work. "
            "It's clearly a manufacturing defect. I've had it for 8 days. "
            "Your policy says 30-day returns. How do I return this and get a replacement or refund?"
        ),
        "true_category": "returns",
        "true_priority": "medium",
        "true_tags": ["defective", "return", "replacement"],
        "customer": {
            "customer_id": "C1007",
            "name": "Aisha Thompson",
            "email": "aisha.t@gmail.com",
            "account_tier": "standard",
            "total_orders": 15,
            "previous_tickets": 2,
            "sentiment": "neutral",
        },
        "ideal_resolution": "fixed",
        "sla_hours": 24,
        "escalation_needed": False,
        "complexity": "easy",
    },

    # ── ACCOUNT tickets ──────────────────────────────────────────────────────
    {
        "id": "T008",
        "subject": "Need to update payment method but can't access settings",
        "body": (
            "I'm trying to update my credit card information but the payment settings page "
            "just shows a loading spinner forever. I've tried Chrome, Firefox, and Safari. "
            "I have a subscription renewing in 2 days and I need to update this urgently."
        ),
        "true_category": "account",
        "true_priority": "high",
        "true_tags": ["account-settings", "payment-method", "bug"],
        "customer": {
            "customer_id": "C1008",
            "name": "David Kim",
            "email": "david.kim@workmail.com",
            "account_tier": "premium",
            "total_orders": 32,
            "previous_tickets": 1,
            "sentiment": "negative",
        },
        "ideal_resolution": "fixed",
        "sla_hours": 4,
        "escalation_needed": True,
        "escalation_team": "engineering",
        "complexity": "medium",
    },

    # ── COMPLAINT tickets ────────────────────────────────────────────────────
    {
        "id": "T009",
        "subject": "Extremely poor customer service experience",
        "body": (
            "I've been a loyal customer for 4 years and have spent thousands of dollars. "
            "Last week I contacted support 3 times about a simple issue and got three different answers. "
            "The agents were dismissive and one actually hung up on me. "
            "I'm seriously considering cancelling my enterprise subscription ($4,800/year). "
            "I need a senior manager to call me back today."
        ),
        "true_category": "complaint",
        "true_priority": "urgent",
        "true_tags": ["churn-risk", "enterprise", "escalation-needed", "vip"],
        "customer": {
            "customer_id": "C1009",
            "name": "Catherine Walsh",
            "email": "c.walsh@techcorp.io",
            "account_tier": "enterprise",
            "total_orders": 148,
            "previous_tickets": 8,
            "sentiment": "angry",
        },
        "ideal_resolution": "explained",
        "sla_hours": 1,
        "escalation_needed": True,
        "escalation_team": "tier2",
        "complexity": "hard",
    },
    {
        "id": "T010",
        "subject": "General question about international shipping",
        "body": (
            "Hi, I'd like to order some items to ship to my family in Canada. "
            "Do you ship internationally? What are the typical delivery times and any customs fees I should expect? "
            "Thanks!"
        ),
        "true_category": "general",
        "true_priority": "low",
        "true_tags": ["international-shipping", "info-request"],
        "customer": {
            "customer_id": "C1010",
            "name": "Robert Nguyen",
            "email": "rnguyen@personal.net",
            "account_tier": "standard",
            "total_orders": 2,
            "previous_tickets": 0,
            "sentiment": "positive",
        },
        "ideal_resolution": "explained",
        "sla_hours": 48,
        "escalation_needed": False,
        "complexity": "easy",
    },
]

# ─── Conversation starters per ticket (customer follow-ups) ───────────────────

CUSTOMER_FOLLOW_UPS: Dict[str, List[str]] = {
    "T001": [
        "Any update on my refund? It's been over an hour.",
        "I just checked my bank and still both charges. Is this being processed?",
        "Can you confirm the refund amount and when I'll receive it?",
    ],
    "T002": [
        "I still haven't heard back. My bank is asking if I want to dispute this.",
        "Can you send me written confirmation that the subscription is cancelled?",
        "How long does the refund take to process?",
    ],
    "T003": [
        "Is there any workaround? I really need to get these photos uploaded today.",
        "Are other iOS 17 users having this issue?",
        "When do you expect a fix?",
    ],
    "T004": [
        "I just tried again and same issue. The link expires instantly.",
        "Can you reset my password manually for me?",
    ],
    "T005": [
        "I checked with my other neighbors too, nobody has it.",
        "Can you file a claim with the carrier on my behalf?",
        "I need this resolved today, it was an important item.",
    ],
    "T006": [
        "The wedding has already passed. A refund is the only acceptable solution now.",
        "Will I also be compensated for the inconvenience?",
    ],
    "T007": [
        "Do I need to pay for return shipping?",
        "Would you prefer I send it back or would you just send a replacement?",
    ],
    "T008": [
        "My subscription renews tomorrow! I really need this fixed today.",
        "Can someone manually update my payment info over the phone?",
    ],
    "T009": [
        "I'm still waiting for a callback. This is getting worse.",
        "I'd like to speak with someone in leadership, not front-line support.",
    ],
    "T010": [
        "Thanks! One more question - do you have any items that are restricted for Canada?",
    ],
}

# ─── Knowledge base articles ──────────────────────────────────────────────────

KNOWLEDGE_BASE: Dict[str, str] = {
    "billing_refund": (
        "Refunds are processed within 3-5 business days to the original payment method. "
        "For duplicate charges, we issue the refund immediately and it appears within 1-3 days."
    ),
    "shipping_lost": (
        "For packages marked delivered but not received: (1) Check with neighbors, (2) "
        "We file a carrier claim within 24h, (3) If unresolved in 5 days, we reship or refund."
    ),
    "password_reset": (
        "Password reset links expire after 15 minutes. If expired, request a new one. "
        "Ensure you're clicking the MOST RECENT email. Check spam folder."
    ),
    "return_policy": (
        "30-day return window for all items. Defective items: free return shipping label provided. "
        "Refund or replacement at customer's choice."
    ),
    "international_shipping": (
        "We ship to Canada, UK, EU, Australia. Delivery: 7-14 business days. "
        "Customs fees vary by country and item type; customer responsibility."
    ),
    "subscription_cancellation": (
        "Cancellations take effect immediately. Refunds for unused periods granted within 7 days "
        "of cancellation, prorated."
    ),
}


def get_ticket_by_id(ticket_id: str) -> Dict[str, Any]:
    """Retrieve a ticket template by its ID."""
    for t in TICKET_TEMPLATES:
        if t["id"] == ticket_id:
            return t
    raise ValueError(f"Ticket {ticket_id} not found")


def get_random_ticket(complexity: str | None = None) -> Dict[str, Any]:
    """Get a random ticket, optionally filtered by complexity."""
    pool = TICKET_TEMPLATES
    if complexity:
        pool = [t for t in pool if t.get("complexity") == complexity]
    return random.choice(pool)


def get_all_ticket_ids() -> List[str]:
    return [t["id"] for t in TICKET_TEMPLATES]


def compute_sla_deadline(sla_hours: int, reference_time: datetime | None = None) -> str:
    """Compute SLA deadline string from hours."""
    if reference_time is None:
        reference_time = datetime.now(timezone.utc)
    deadline = reference_time + timedelta(hours=sla_hours)
    return deadline.isoformat()


def time_to_sla_human(deadline_str: str, now: datetime | None = None) -> str:
    """Return human-readable time until SLA deadline."""
    if now is None:
        now = datetime.now(timezone.utc)
    try:
        deadline = datetime.fromisoformat(deadline_str)
        if deadline.tzinfo is None:
            deadline = deadline.replace(tzinfo=timezone.utc)
        delta = deadline - now
        if delta.total_seconds() < 0:
            return "SLA BREACHED"
        hours = int(delta.total_seconds() // 3600)
        minutes = int((delta.total_seconds() % 3600) // 60)
        if hours > 0:
            return f"{hours}h {minutes}m remaining"
        return f"{minutes}m remaining"
    except Exception:
        return "Unknown"