import logging
from dataclasses import dataclass
from typing import Optional

from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit
from scrobbles.dataclasses import BaseLogData
from scrobbles.constants import MediaType
from scrobbles.mixins import ScrobblableConstants, ScrobblableItem

logger = logging.getLogger(__name__)
BNULL = {"blank": True, "null": True}
User = get_user_model()


@dataclass
class MoodLogData(BaseLogData):
    reasons: Optional[str] = None


class Mood(ScrobblableItem):
    MEDIA_TYPE = MediaType.MOOD

    scrobblableitem_ptr = models.OneToOneField(
        ScrobblableItem,
        parent_link=True,
        primary_key=True,
        on_delete=models.CASCADE,
        related_name="as_mood",
    )

    image = models.ImageField(upload_to="moods/", **BNULL)
    image_small = ImageSpecField(
        source="thumbnail",
        processors=[ResizeToFit(100, 100)],
        format="JPEG",
        options={"quality": 60},
    )
    image_medium = ImageSpecField(
        source="thumbnail",
        processors=[ResizeToFit(300, 300)],
        format="JPEG",
        options={"quality": 75},
    )

    def get_absolute_url(self):
        return reverse("moods:mood_detail", kwargs={"slug": self.uuid})

    @property
    def subtitle(self) -> str:
        return ""

    @property
    def strings(self) -> ScrobblableConstants:
        return ScrobblableConstants(verb="Feeling", tags="thinking")

    @property
    def logdata_cls(self):
        return MoodLogData

    @property
    def primary_image_url(self) -> str:
        if self.image:
            return self.image.url
        return ""
