"""
QUICK CAPTURE — Phase 11, and the one list in the app that is NOT scoped by a
page.

The contract is `front/src/renderer/src/features/quick-capture/data/api.js`.
Where this file disagrees with it, this file is wrong.

**THE INBOX BELONGS TO THE USER, NOT TO A PAGE OR A SPACE, AND THAT IS THE
WHOLE FEATURE.** Every other row in this schema hangs off a `Page`, because
every other feature is something you go to. Capture is the opposite: you write
the thought down NOW and decide what it is later, and asking "which Space?"
while the thought is still warm is exactly the friction this exists to remove.
So `Capture` has a `user` foreign key and nothing else above it, `/api/captures/`
takes no `?page=`, and there is no bulk endpoint — a single global inbox has no
per-page list to snapshot and replace.

Three things this model states deliberately:

1. **`pinned` HAS TO ROUND-TRIP.** The inbox is ordered by recency and nothing
   else, which is right for capturing and wrong for coming back — so this is
   the one piece of ordering the user gets. Lose it on the round trip and the
   pin silently comes undone on the next fetch.

2. **A CAPTURE WITH NO TITLE IS THE NORMAL SHAPE, not an edge case.** Asking
   for a title is the friction the feature exists to remove; three of the five
   examples have none. `title` is therefore blank-able and nothing anywhere
   substitutes a placeholder for it.

3. **`triaged_to` IS A FOREIGN KEY WITH `SET_NULL`, AND THE PAGE'S NAME IS
   DERIVED.** A capture that has been dealt with is drawn differently instead
   of sitting in the inbox forever. Deleting the destination page therefore
   returns the capture to the inbox rather than leaving it pointing at a page
   that is gone — the pointer is what "dealt with" MEANS here, so a dangling
   one would be a capture claiming to have been filed somewhere unnameable.
   The alternative, storing the page's name alongside the id, would keep the
   label and lose the link, which is the worse half to keep.
"""

from django.conf import settings as django_settings
from django.db import models

from core.models import TimeStampedModel

from .workspace import Page


class Capture(TimeStampedModel):
    # STRAIGHT TO THE USER. No page, no space — see the module docstring.
    user = models.ForeignKey(
        django_settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='captures',
    )

    # Blank is the normal shape, not a missing value.
    title = models.CharField(max_length=300, blank=True)
    body = models.TextField(blank=True)

    pinned = models.BooleanField(default=False)

    # Where this capture was sent on to, once it has been. Null is the inbox.
    triaged_to = models.ForeignKey(
        Page,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='captures',
    )

    is_sample = models.BooleanField(default=False)

    class Meta:
        # Newest first: the inbox is read from the top and the thing you just
        # wrote must be the thing you see. **PINNING IS NOT IN THIS ORDER** —
        # it is applied by the client, which draws pinned captures above the
        # rest, because "kept at the top" is a view decision and the recency
        # order underneath it has to stay intact.
        ordering = ['-created_at', '-id']
        indexes = [
            models.Index(fields=['user', '-created_at']),
        ]

    def __str__(self):
        return self.title or (self.body[:40] or f'(empty capture {self.pk})')
