from django.core.management.base import BaseCommand
from django.utils import timezone
from django.contrib.auth.models import User
from events.models import Event, CallForProposal, Proposal, Publication
from django.core.files.base import ContentFile

class Command(BaseCommand):
    help = 'Creates test publications and proposals'

    def handle(self, *args, **kwargs):
        # Create test user if not exists
        user, created = User.objects.get_or_create(
            username='testuser',
            defaults={
                'email': 'test@example.com',
                'is_staff': True
            }
        )
        if created:
            user.set_password('testpass123')
            user.save()
            self.stdout.write(self.style.SUCCESS(f'Created test user: {user.username}'))

        # Create past event (more than 2 hours ago)
        past_event = Event.objects.create(
            title='Past Test Event',
            description='A test event that happened more than 2 hours ago',
            date_time=timezone.now() - timezone.timedelta(hours=3),
            venue='Test Venue'
        )
        self.stdout.write(self.style.SUCCESS(f'Created past event: {past_event.title}'))

        # Create future event
        future_event = Event.objects.create(
            title='Future Test Event',
            description='A test event that will happen in the future',
            date_time=timezone.now() + timezone.timedelta(days=1),
            venue='Test Venue'
        )
        self.stdout.write(self.style.SUCCESS(f'Created future event: {future_event.title}'))

        # Create CFP for past event
        past_cfp = CallForProposal.objects.create(
            event=past_event,
            title='Past Test CFP',
            description='Test CFP for past event',
            submission_deadline=timezone.now() - timezone.timedelta(days=1),
            status='closed',
            created_by=user
        )

        # Create standalone publication for past event
        test_file = ContentFile(b'Test publication content', name='test_publication.pdf')
        past_publication = Publication.objects.create(
            title='Test Past Publication',
            abstract='This is a test standalone publication',
            author=user,
            publication_type='document',
            event=past_event,
            is_public=True
        )
        past_publication.file.save('test_publication.pdf', test_file)
        past_publication.save()
        self.stdout.write(self.style.SUCCESS(f'Created standalone publication: {past_publication.title}'))

        # Create standalone publication for future event (should not be visible)
        test_future_file = ContentFile(b'Test future publication content', name='test_future_publication.pdf')
        future_publication = Publication.objects.create(
            title='Test Future Publication',
            abstract='This is a test publication for a future event',
            author=user,
            publication_type='presentation',
            event=future_event,
            is_public=True
        )
        future_publication.file.save('test_future_publication.pdf', test_future_file)
        future_publication.save()
        self.stdout.write(self.style.SUCCESS(f'Created future publication: {future_publication.title}'))

        # Create standalone publication with no event
        test_standalone_file = ContentFile(b'Test standalone content', name='test_standalone.pdf')
        standalone_publication = Publication.objects.create(
            title='Test Standalone Publication',
            abstract='This is a test publication not tied to any event',
            author=user,
            publication_type='paper',
            is_public=True
        )
        standalone_publication.file.save('test_standalone.pdf', test_standalone_file)
        standalone_publication.save()
        self.stdout.write(self.style.SUCCESS(f'Created standalone publication: {standalone_publication.title}'))
