"""
Report models for the Dubai Analytics Platform.
"""
from django.db import models
from django.contrib.auth import get_user_model
from apps.core.models import TimeStampedModel
import uuid

User = get_user_model()


class Report(TimeStampedModel):
    """Generated reports."""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reports')
    title = models.CharField(max_length=255)
    report_type = models.CharField(max_length=50, choices=[
        ('transaction_summary', 'Transaction Summary'),
        ('area_analysis', 'Area Analysis'),
        ('market_trends', 'Market Trends'),
        ('forecast_report', 'Forecast Report'),
        ('custom', 'Custom Report'),
    ])
    format = models.CharField(max_length=10, choices=[
        ('pdf', 'PDF'),
        ('excel', 'Excel'),
        ('csv', 'CSV'),
    ])
    status = models.CharField(max_length=20, choices=[
        ('pending', 'Pending'),
        ('processing', 'Processing'),
        ('completed', 'Completed'),
        ('failed', 'Failed'),
    ], default='pending')
    file_path = models.CharField(max_length=500, blank=True)
    file_size = models.BigIntegerField(null=True, blank=True)
    parameters = models.JSONField(default=dict)
    error_message = models.TextField(blank=True)
    expires_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = 'reports'
        indexes = [
            models.Index(fields=['user', 'created_at']),
            models.Index(fields=['status']),
            models.Index(fields=['report_type']),
        ]

    def __str__(self):
        return f"{self.title} - {self.user.email}"


class ReportTemplate(TimeStampedModel):
    """Report templates for customization."""
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    report_type = models.CharField(max_length=50)
    template_config = models.JSONField(default=dict)
    is_active = models.BooleanField(default=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_templates')

    class Meta:
        db_table = 'report_templates'

    def __str__(self):
        return self.name

