"""
Publishing Models
Phase 5: Sites Renderer & Publishing
"""
from django.db import models
from igny8_core.auth.models import SiteSectorBaseModel


class PublishingRecord(SiteSectorBaseModel):
    """
    Track content publishing to various destinations.
    """
    
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('publishing', 'Publishing'),
        ('published', 'Published'),
        ('failed', 'Failed'),
    ]
    
    # Content reference
    content = models.ForeignKey(
        'writer.Content',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='publishing_records',
        help_text="Content being published"
    )
    
    # Destination information
    destination = models.CharField(
        max_length=50,
        db_index=True,
        help_text="Destination platform: 'wordpress', 'sites', 'shopify'"
    )
    destination_id = models.CharField(
        max_length=255,
        blank=True,
        null=True,
        help_text="External ID in destination platform"
    )
    destination_url = models.URLField(
        blank=True,
        null=True,
        help_text="URL of published content/site"
    )
    
    # Status tracking
    status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES,
        default='pending',
        db_index=True
    )
    published_at = models.DateTimeField(null=True, blank=True)
    error_message = models.TextField(blank=True, null=True)
    
    # Metadata
    metadata = models.JSONField(
        default=dict,
        help_text="Platform-specific metadata"
    )
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        app_label = 'publishing'
        db_table = 'igny8_publishing_records'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['destination', 'status']),
            models.Index(fields=['content', 'destination']),
            models.Index(fields=['account', 'status']),
        ]
    
    def __str__(self):
        return f"{self.content} → {self.destination} ({self.get_status_display()})"


class DeploymentRecord(SiteSectorBaseModel):
    """
    Track site deployments to Sites renderer.
    Legacy model - SiteBlueprint functionality removed.
    """
    
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('deploying', 'Deploying'),
        ('deployed', 'Deployed'),
        ('failed', 'Failed'),
        ('rolled_back', 'Rolled Back'),
    ]
    
    # Legacy: site_blueprint field removed - now using site from SiteSectorBaseModel directly
    
    # Version tracking
    version = models.IntegerField(
        help_text="Blueprint version being deployed"
    )
    deployed_version = models.IntegerField(
        null=True,
        blank=True,
        help_text="Currently deployed version (after successful deployment)"
    )
    
    # Status tracking
    status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES,
        default='pending',
        db_index=True
    )
    deployed_at = models.DateTimeField(null=True, blank=True)
    deployment_url = models.URLField(
        blank=True,
        null=True,
        help_text="Public URL of deployed site"
    )
    error_message = models.TextField(blank=True, null=True)
    
    # Metadata
    metadata = models.JSONField(
        default=dict,
        help_text="Deployment metadata (build info, file paths, etc.)"
    )
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        app_label = 'publishing'
        db_table = 'igny8_deployment_records'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['status']),
            models.Index(fields=['account', 'status']),
            models.Index(fields=['site', 'status']),
        ]
    
    def __str__(self):
        return f"Deployment v{self.version} for {self.site.name if self.site else 'Unknown'} ({self.get_status_display()})"


