import sys
import json
from colleges.models import College
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'Read and display college data for a given slug.'

    def add_arguments(self, parser):
        parser.add_argument('--slug', type=str, required=True, help='College slug')

    def handle(self, *args, **options):
        slug = options['slug']
        try:
            college = College.objects.get(slug=slug)
            self.stdout.write(json.dumps({
                'name': college.name,
                'slug': college.slug,
                'description': college.description,
                'address': college.address,
                'contact': college.contact,
                'website': college.website,
                'email': college.email,
                'phone': college.phone,
                'established': college.established,
                'affiliation': college.affiliation,
                'courses_offered': [c.slug for c in college.courses.all()] if hasattr(college, 'courses') else []
            }, indent=2))
        except College.DoesNotExist:
            self.stdout.write(self.style.ERROR(f"College with slug '{slug}' does not exist."))
