"""
Initial migration for Themis — creates UserProfile and UserAPIKey tables.
"""
import uuid

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="UserProfile",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "home_timezone",
                    models.CharField(
                        default="UTC",
                        help_text="User's home/permanent timezone",
                        max_length=50,
                    ),
                ),
                (
                    "current_timezone",
                    models.CharField(
                        blank=True,
                        default="",
                        help_text="User's current timezone when traveling (leave blank if at home)",
                        max_length=50,
                    ),
                ),
                (
                    "date_format",
                    models.CharField(
                        choices=[
                            ("YYYY-MM-DD", "2024-12-25"),
                            ("DD/MM/YYYY", "25/12/2024"),
                            ("MM/DD/YYYY", "12/25/2024"),
                            ("DD.MM.YYYY", "25.12.2024"),
                            ("DD-MM-YYYY", "25-12-2024"),
                        ],
                        default="YYYY-MM-DD",
                        help_text="Preferred date display format",
                        max_length=20,
                    ),
                ),
                (
                    "time_format",
                    models.CharField(
                        choices=[
                            ("12-hour", "12-hour (3:30 PM)"),
                            ("24-hour", "24-hour (15:30)"),
                        ],
                        default="24-hour",
                        help_text="12-hour or 24-hour time format",
                        max_length=10,
                    ),
                ),
                (
                    "thousand_separator",
                    models.CharField(
                        choices=[
                            ("comma", "Comma (1,000)"),
                            ("period", "Period (1.000)"),
                            ("space", "Space (1 000)"),
                            ("none", "None (1000)"),
                        ],
                        default="comma",
                        help_text="Number formatting preference",
                        max_length=10,
                    ),
                ),
                (
                    "week_start",
                    models.CharField(
                        choices=[
                            ("monday", "Monday"),
                            ("sunday", "Sunday"),
                            ("saturday", "Saturday"),
                        ],
                        default="monday",
                        help_text="First day of the week",
                        max_length=10,
                    ),
                ),
                (
                    "theme_mode",
                    models.CharField(
                        choices=[
                            ("light", "Light"),
                            ("dark", "Dark"),
                            ("auto", "Auto (System)"),
                        ],
                        default="auto",
                        help_text="Theme mode: light, dark, or auto (follows system)",
                        max_length=10,
                    ),
                ),
                (
                    "theme_name",
                    models.CharField(
                        default="corporate",
                        help_text="DaisyUI theme for light mode",
                        max_length=30,
                    ),
                ),
                (
                    "dark_theme_name",
                    models.CharField(
                        default="business",
                        help_text="DaisyUI theme for dark mode",
                        max_length=30,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="profile",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "User Profile",
                "verbose_name_plural": "User Profiles",
            },
        ),
        migrations.CreateModel(
            name="UserAPIKey",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "service_name",
                    models.CharField(
                        help_text="Service this key belongs to (e.g. OpenAI, Anthropic, CalDAV)",
                        max_length=100,
                    ),
                ),
                (
                    "key_type",
                    models.CharField(
                        choices=[
                            ("api", "API Key"),
                            ("mcp", "MCP Server"),
                            ("dav", "DAV Credentials"),
                            ("token", "Access Token"),
                            ("secret", "Secret Key"),
                            ("other", "Other"),
                        ],
                        default="api",
                        help_text="Type of credential",
                        max_length=30,
                    ),
                ),
                (
                    "label",
                    models.CharField(
                        blank=True,
                        default="",
                        help_text="Your nickname for this key (e.g. 'Work account')",
                        max_length=100,
                    ),
                ),
                (
                    "encrypted_value",
                    models.TextField(help_text="Fernet-encrypted credential value"),
                ),
                (
                    "instructions",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="How to obtain and use this key",
                    ),
                ),
                (
                    "help_url",
                    models.URLField(
                        blank=True,
                        default="",
                        help_text="Link to service documentation",
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True,
                        help_text="Whether this key is currently in use",
                    ),
                ),
                (
                    "last_used_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Last time this key was used",
                        null=True,
                    ),
                ),
                (
                    "expires_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="When this key expires (if applicable)",
                        null=True,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="api_keys",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "API Key",
                "verbose_name_plural": "API Keys",
            },
        ),
    ]
