import yaml
from django.core.management.base import BaseCommand
from backend.core.models import DocumentTemplate

class Command(BaseCommand):
    help = "Export document template to a YAML file"

    def add_arguments(self, parser):
        parser.add_argument('yaml_file', type=str, help="The YAML file to export template to")

    def handle(self, *args, **options):
        yaml_file_path = options['yaml_file']

        template = DocumentTemplate.objects.first()

        if template:
            with open(yaml_file_path, 'w') as yaml_file:
                yaml_file.write(template.content)

            self.stdout.write(self.style.SUCCESS('Template exported successfully'))
        else:
            self.stdout.write(self.style.ERROR('No template found in the database'))
