from __future__ import annotations

import uuid
from typing import ClassVar

from django.core.exceptions import ObjectDoesNotExist
from django.db import models

# Create your models here.


class PrismaMigrations(models.Model):
    objects: ClassVar[models.Manager[PrismaMigrations]]
    id = models.CharField(primary_key=True, max_length=36)
    checksum = models.CharField(max_length=64)
    finished_at = models.DateTimeField(blank=True, null=True)
    migration_name = models.CharField(max_length=255)
    logs = models.TextField(blank=True, null=True)
    rolled_back_at = models.DateTimeField(blank=True, null=True)
    started_at = models.DateTimeField()
    applied_steps_count = models.IntegerField()

    class Meta:
        managed = False
        db_table = "_prisma_migrations"


class CFAPIKeys(models.Model):
    objects: ClassVar[models.Manager[CFAPIKeys]]
    DoesNotExist: ClassVar[type[ObjectDoesNotExist]]
    id = models.AutoField(primary_key=True)
    key = models.CharField(max_length=255, unique=True)
    suffix = models.CharField(max_length=4)
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    last_used = models.DateTimeField(null=True, blank=True)
    user_id = models.TextField(null=True, blank=True)
    tier = models.TextField(null=True, blank=True)
    organization_id = models.TextField(null=True, blank=True)

    class Meta:
        managed = False
        db_table = "cf_api_keys"


class Subscriptions(models.Model):
    objects: ClassVar[models.Manager[Subscriptions]]
    id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
    user_id = models.TextField(unique=True)
    stripe_customer_id = models.TextField(null=True, blank=True, unique=True)
    stripe_subscription_id = models.TextField(null=True, blank=True)
    plan_type = models.TextField()
    optimizations_used = models.IntegerField(default=0)
    total_lifetime_optimizations = models.IntegerField(default=0)
    optimizations_limit = models.IntegerField()
    subscription_status = models.TextField()
    current_period_start = models.DateTimeField(null=True, blank=True)
    current_period_end = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    cancel_at_period_end = models.BooleanField(default=False)
    cancellation_request_date = models.DateTimeField(null=True, blank=True)

    class Meta:
        managed = False
        db_table = "subscriptions"


# Users model
class Users(models.Model):
    objects: ClassVar[models.Manager[Users]]
    user_id = models.CharField(primary_key=True, max_length=255)
    github_username = models.CharField(max_length=255, unique=True)
    email = models.EmailField(null=True, blank=True)
    name = models.CharField(max_length=255, null=True, blank=True)
    onboarding_completed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        managed = False
        db_table = "users"


class Organizations(models.Model):
    objects: ClassVar[models.Manager[Organizations]]
    id = models.CharField(primary_key=True, max_length=36)
    name = models.CharField(max_length=255, unique=True)
    github_org_id = models.CharField(max_length=255, null=True, blank=True, unique=True)
    description = models.TextField(null=True, blank=True)
    website = models.CharField(max_length=255, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    auto_add_github_members = models.BooleanField(default=True)
    added_by = models.TextField(null=True, blank=True)
    privacy_mode = models.BooleanField(default=False)
    subscription = models.BooleanField(default=False)

    class Meta:
        managed = False
        db_table = "organizations"
