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
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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
"""Structured logs for task-creation source URLs.

Every submitted source is logged once. Rejected attempts are also stored in
``rejected_source_attempts`` so they can be queried without reading the raw
request body. Query strings, fragments, and credentials are removed; only a
few public content identifiers (YouTube ``v`` / ``t`` / ``list``, a VOD
timestamp, a Kick clip id) are kept.
"""

from __future__ import annotations

import logging
import re
from dataclasses import dataclass
from typing import Any
from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit, urlunsplit
from uuid import uuid4

from .workflows.contracts import WORKFLOWS

logger = logging.getLogger(__name__)

_IOS_CLIENT = re.compile(r"supoclip-ios/(\d{1,8}(?:\.\d{1,8}){0,4})", re.IGNORECASE)
_URL_IN_TEXT = re.compile(r"https?://\S+", re.IGNORECASE)
_MAX_URL_LENGTH = 2000
_MAX_REASON_LENGTH = 500


@dataclass(frozen=True)
class SanitizedSource:
    sanitized_url: str
    domain: str
    source_type: str


def client_label(user_agent: str | None) -> str:
    """Return ``web`` or the iOS client token (``supoclip-ios/N``)."""
    if not user_agent:
        return "web"
    match = _IOS_CLIENT.search(user_agent)
    if not match:
        return "web"
    return f"supoclip-ios/{match.group(1)}"


def is_task_create_request(method: str, path: str) -> bool:
    normalized = (path or "").rstrip("/") or "/"
    return method.upper() == "POST" and normalized == "/tasks"


def sanitize_source_url(raw: str | None) -> SanitizedSource:
    """Keep scheme, host, and path. Drop secrets, fragments, and credentials."""
    text = _strip_controls(str(raw or "").strip())
    if len(text) > 8000:
        text = text[:8000]
    if not text:
        return SanitizedSource("", "unknown", "unsupported")

    lower = text.lower()
    if lower.startswith("upload://"):
        return _sanitize_upload(text)
    if lower.startswith("data:"):
        return SanitizedSource("data:", "data", "unsupported")
    if "://" not in text:
        return SanitizedSource("local-path", "local", "unsupported")

    scheme = re.sub(r"[^a-z0-9+.-]", "", lower.split(":", 1)[0])[:32] or "invalid"
    if scheme not in {"http", "https"}:
        return SanitizedSource(f"{scheme}:", scheme, "unsupported")
    return _sanitize_http(text, scheme)


def workflow_label(raw: Any) -> str:
    """The requested workflow id. Missing means highlights; anything unknown is
    ``invalid``, so request text never reaches the logs."""
    if raw is None:
        return "highlights"
    return raw if isinstance(raw, str) and raw in WORKFLOWS else "invalid"


def record_fields(
    *,
    raw_url: str | None,
    user_id: str | None,
    user_agent: str | None,
    outcome: str,
    status_code: int,
    detail: Any = None,
    allow_empty_url: bool = False,
    workflow: Any = None,
) -> dict[str, Any] | None:
    """Build the structured fields for one submission, or None when nothing was submitted."""
    url = raw_url.strip() if isinstance(raw_url, str) else ""
    if not url and not allow_empty_url:
        return None

    described = sanitize_source_url(url) if url else SanitizedSource("", "unknown", "unsupported")
    reason = _rejection_reason(status_code, detail) if outcome == "rejected" else None
    safe_user = user_id.strip()[:255] if isinstance(user_id, str) and user_id.strip() else None
    return {
        "user_id": safe_user,
        "client": client_label(user_agent),
        "source_type": described.source_type,
        "domain": described.domain or "unknown",
        "sanitized_url": described.sanitized_url,
        "outcome": outcome,
        "status_code": int(status_code),
        "reason": reason,
        "workflow": workflow_label(workflow),
    }


def log_source_submission(fields: dict[str, Any]) -> None:
    """Emit one JSON log line. The message itself is single-line and sanitized."""
    reason = fields.get("reason") or "-"
    extra = {
        "source_user_id": fields.get("user_id") or "-",
        "source_client": fields["client"],
        "source_type": fields["source_type"],
        "source_domain": fields["domain"],
        "source_url": fields["sanitized_url"] or "-",
        "source_outcome": fields["outcome"],
        "source_status": fields["status_code"],
        "source_workflow": fields.get("workflow") or "highlights",
    }
    if fields["outcome"] == "rejected" and fields.get("reason"):
        extra["source_reason"] = fields["reason"]
    level = logging.WARNING if fields["outcome"] == "rejected" else logging.INFO
    logger.log(
        level,
        "source_submission outcome=%s status=%s user_id=%s client=%s source_type=%s domain=%s url=%s reason=%s",
        fields["outcome"],
        fields["status_code"],
        fields.get("user_id") or "-",
        fields["client"],
        fields["source_type"],
        fields["domain"],
        fields["sanitized_url"] or "-",
        reason,
        extra=extra,
    )


async def record_source_submission(
    *,
    raw_url: str | None,
    user_id: str | None,
    user_agent: str | None,
    outcome: str,
    status_code: int,
    detail: Any = None,
    persist: bool = False,
    allow_empty_url: bool = False,
    workflow: Any = None,
) -> None:
    """Log one submission. Persist only rejected attempts, and only when asked."""
    try:
        fields = record_fields(
            raw_url=raw_url,
            user_id=user_id,
            user_agent=user_agent,
            outcome=outcome,
            status_code=status_code,
            detail=detail,
            allow_empty_url=allow_empty_url,
            workflow=workflow,
        )
        if fields is None:
            return
        log_source_submission(fields)
        if persist and outcome == "rejected":
            await _persist_rejection(fields)
    except Exception as exc:
        # The exception text can echo the submitted URL, so keep only the type.
        logger.warning("source_submission log failed: %s", type(exc).__name__)


async def log_task_create_validation_error(request: Any, exc: Any) -> None:
    """Log a FastAPI/Pydantic validation failure for ``POST /tasks``."""
    if not is_task_create_request(
        getattr(request, "method", ""),
        str(getattr(getattr(request, "url", None), "path", "")),
    ):
        return
    state = getattr(request, "state", None)
    user_id = getattr(state, "user_id", None)
    headers = getattr(request, "headers", None)
    user_agent = headers.get("user-agent") if headers is not None and hasattr(headers, "get") else None
    await record_source_submission(
        raw_url=_url_from_validation_body(getattr(exc, "body", None)),
        user_id=user_id if isinstance(user_id, str) else None,
        user_agent=user_agent if isinstance(user_agent, str) else None,
        outcome="rejected",
        status_code=422,
        detail=_validation_reason(exc),
        persist=True,
        allow_empty_url=True,
    )


class SourceAttempt:
    """Tracks the source URL seen while handling one create-task request."""

    def __init__(self, request: Any, db: Any):
        self.db = db
        self.user_agent = _header(request, "user-agent")
        self.raw_url: str | None = None
        self.user_id: str | None = None
        self.workflow: Any = None
        self._finished = False

    def note_url(self, raw_url: Any) -> None:
        if isinstance(raw_url, str) and raw_url.strip():
            self.raw_url = raw_url.strip()

    def note_user(self, user_id: str | None) -> None:
        if isinstance(user_id, str) and user_id.strip():
            self.user_id = user_id.strip()

    def note_workflow(self, workflow: Any) -> None:
        """The raw requested workflow; only its known id (or ``invalid``) is logged."""
        self.workflow = workflow

    async def accepted(self) -> None:
        await self._finish("accepted", 200, None)

    async def rejected(self, status_code: int, detail: Any) -> None:
        await self._finish("rejected", status_code, detail)

    async def _finish(self, outcome: str, status_code: int, detail: Any) -> None:
        if self._finished:
            return
        self._finished = True
        await record_source_submission(
            raw_url=self.raw_url,
            user_id=self.user_id,
            user_agent=self.user_agent,
            outcome=outcome,
            status_code=status_code,
            detail=detail,
            persist=outcome == "rejected" and self.db is not None,
            workflow=self.workflow,
        )


def _header(request: Any, name: str) -> str | None:
    headers = getattr(request, "headers", None)
    getter = getattr(headers, "get", None)
    if not callable(getter):
        return None
    value = getter(name)
    return value if isinstance(value, str) else None


def _strip_controls(value: str) -> str:
    return "".join(ch for ch in value if ord(ch) >= 32 and ord(ch) != 127)


def _sanitize_upload(url: str) -> SanitizedSource:
    """Log the file name only. Object keys can embed the owner id; contents stay unread."""
    without_query = url.split("#", 1)[0].split("?", 1)[0]
    name = unquote(without_query.rstrip("/").rsplit("/", 1)[-1])
    name = "".join(ch if ch.isalnum() or ch in "._-" else "_" for ch in name)[:80]
    if not name or name in {".", ".."} or name.lower() == "upload:":
        name = "file"
    return SanitizedSource(f"upload://{name}", "upload", "video_url")


def _sanitize_http(text: str, scheme: str) -> SanitizedSource:
    invalid = SanitizedSource(f"{scheme}://invalid", "invalid", "unsupported")
    try:
        parts = urlsplit(text)
        port = parts.port
    except ValueError:
        return invalid
    host = (parts.hostname or "").rstrip(".").lower()
    try:
        host = host.encode("idna").decode("ascii")
    except Exception:
        return invalid
    if not _safe_host(host):
        return invalid

    if ":" in host:
        netloc = f"[{host}]"
    else:
        netloc = host
    if port and not ((scheme == "https" and port == 443) or (scheme == "http" and port == 80)):
        netloc = f"{netloc}:{port}"

    sanitized = urlunsplit((scheme, netloc, _safe_path(parts.path), _safe_query(host, parts.query), ""))
    if len(sanitized) > _MAX_URL_LENGTH:
        sanitized = sanitized[:_MAX_URL_LENGTH]
    return SanitizedSource(sanitized, _domain(host), _classify_source(text))


def _safe_host(host: str) -> bool:
    if not host or len(host) > 253 or ".." in host or host.startswith(".") or host.endswith("."):
        return False
    return all(ch.isalnum() or ch in ".-:" for ch in host)


def _domain(host: str) -> str:
    if host.startswith("www."):
        host = host[4:]
    return host[:255] or "unknown"


def _safe_path(path: str) -> str:
    decoded = _strip_controls(unquote(path or ""))
    if len(decoded) > 1024:
        decoded = decoded[:1024]
    pieces = [quote(piece, safe="@:$&'*+,;=-._~") for piece in decoded.split("/")]
    safe = "/".join(pieces)
    if decoded.startswith("/") and not safe.startswith("/"):
        safe = "/" + safe
    return safe


def _safe_query(host: str, query: str) -> str:
    domain = _domain(host)
    allowed: set[str] = set()
    if domain == "youtu.be" or domain.endswith("youtube.com") or domain.endswith("youtube-nocookie.com"):
        allowed = {"v", "t", "list"}
    elif domain.endswith("twitch.tv"):
        allowed = {"t"}
    elif domain == "kick.com" or domain.endswith(".kick.com"):
        allowed = {"t", "clip"}
    if not allowed or not query:
        return ""

    kept: list[tuple[str, str]] = []
    seen: set[str] = set()
    for key, value in parse_qsl(query, keep_blank_values=False):
        normalized = key.lower()
        if normalized not in allowed or normalized in seen:
            continue
        if not _harmless_query_value(normalized, value):
            continue
        seen.add(normalized)
        kept.append((normalized, value))
    return urlencode(kept)


def _harmless_query_value(key: str, value: str) -> bool:
    if any(ord(ch) < 32 or ord(ch) == 127 for ch in value):
        return False
    if key == "v":
        return bool(re.fullmatch(r"[A-Za-z0-9_-]{11}", value))
    if key == "list":
        return bool(re.fullmatch(r"[A-Za-z0-9_-]{8,64}", value))
    if key == "t":
        return bool(re.fullmatch(r"\d{1,6}|(?:\d{1,4}[hms]){1,3}", value))
    if key == "clip":
        return bool(re.fullmatch(r"clip_[A-Za-z0-9_-]{1,80}", value))
    return False


def _classify_source(url: str) -> str:
    stripped = url.strip()
    lower = stripped.lower()
    if lower.startswith("upload://"):
        return "video_url"
    try:
        from .services.video_service import VideoService

        return VideoService.determine_source_type(stripped)
    except Exception:
        host = ""
        try:
            host = (urlsplit(stripped).hostname or "").lower().removeprefix("www.")
        except ValueError:
            host = ""
        if host == "youtu.be" or host.endswith("youtube.com") or host.endswith("youtube-nocookie.com"):
            return "youtube"
        if host.endswith("twitch.tv"):
            return "twitch"
        if host == "kick.com" or host.endswith(".kick.com"):
            return "kick"
        return "unsupported"


def _rejection_reason(status_code: int, detail: Any) -> str:
    text = _reason_text(detail)
    combined = f"{status_code}: {text}" if text else str(status_code)
    return combined[:_MAX_REASON_LENGTH]


def _reason_text(detail: Any) -> str:
    if isinstance(detail, dict):
        raw = detail.get("message") or detail.get("code") or ""
        text = str(raw)
    elif detail is None:
        text = ""
    else:
        text = str(detail)

    def replace(match: re.Match[str]) -> str:
        sanitized = sanitize_source_url(match.group(0)).sanitized_url
        return sanitized or "[url]"

    text = _URL_IN_TEXT.sub(replace, text)
    return " ".join(text.split())[:300]


def _validation_reason(exc: Any) -> str:
    try:
        errors = exc.errors()
    except Exception:
        return "request validation failed"
    parts: list[str] = []
    for error in list(errors)[:5]:
        loc = ".".join(str(item) for item in error.get("loc", ()) if item != "body")
        kind = str(error.get("type") or "invalid")
        parts.append(f"{loc or 'body'}: {kind}"[:120])
    return "; ".join(parts) or "request validation failed"


def _url_from_validation_body(body: Any) -> str | None:
    if isinstance(body, (bytes, bytearray)):
        try:
            import json

            body = json.loads(body.decode("utf-8", errors="ignore"))
        except Exception:
            return None
    if isinstance(body, str):
        try:
            import json

            body = json.loads(body)
        except Exception:
            return None
    if not isinstance(body, dict):
        return None
    source = body.get("source")
    if isinstance(source, dict) and isinstance(source.get("url"), str):
        return source["url"]
    url = body.get("url")
    return url if isinstance(url, str) else None


async def _persist_rejection(fields: dict[str, Any]) -> None:
    try:
        from sqlalchemy import text

        from .database import AsyncSessionLocal

        async with AsyncSessionLocal() as session:
            await session.execute(
                text(
                    """
                    INSERT INTO rejected_source_attempts (
                        id, user_id, domain, sanitized_url, reason, client, created_at
                    )
                    VALUES (
                        :id, :user_id, :domain, :sanitized_url, :reason, :client, CURRENT_TIMESTAMP
                    )
                    """
                ),
                {
                    "id": str(uuid4()),
                    "user_id": fields.get("user_id"),
                    "domain": (fields.get("domain") or "unknown")[:255],
                    "sanitized_url": (fields.get("sanitized_url") or "")[:_MAX_URL_LENGTH],
                    "reason": (fields.get("reason") or "")[:_MAX_REASON_LENGTH],
                    "client": (fields.get("client") or "web")[:128],
                },
            )
            await session.commit()
    except Exception as exc:
        # Database errors quote the bound URL. Log the type only.
        logger.warning("Could not persist rejected source attempt: %s", type(exc).__name__)