from django.core.management.base import BaseCommand
from apps.surveys.models import SurveyTemplate, SurveyQuestion
from apps.organizations.models import Hospital


class Command(BaseCommand):
    help = 'Create a demo survey template with different question types'

    def handle(self, *args, **options):
        # Get or create a hospital
        hospital, _ = Hospital.objects.get_or_create(
            code='HH',
            defaults={
                'name': "Alhammadi Hospital",
                'city': 'Riyadh',
                'status': 'active'
            }
        )

        # Create the survey template
        template = SurveyTemplate.objects.create(
            name="Patient Experience Demo Survey",
            name_ar="استبيان تجربة المريض التجريبي",
            hospital=hospital,
            survey_type='stage',
            scoring_method='average',
            negative_threshold=3.0,
            is_active=True
        )

        self.stdout.write(self.style.SUCCESS(f'✓ Created template: {template.name}'))

        # Question 1: Text question
        q1 = SurveyQuestion.objects.create(
            survey_template=template,
            text="Please share any additional comments about your stay",
            text_ar="يرجى مشاركة أي تعليقات إضافية حول إقامتك",
            question_type='text',
            order=1,
            is_required=False
        )
        self.stdout.write(f'  ✓ Question 1: Text - {q1.text}')

        # Question 2: Rating question
        q2 = SurveyQuestion.objects.create(
            survey_template=template,
            text="How would you rate the quality of nursing care?",
            text_ar="كيف تقي جودة التمريض؟",
            question_type='rating',
            order=2,
            is_required=True
        )
        self.stdout.write(f'  ✓ Question 2: Rating - {q2.text}')

        # Question 3: Multiple choice question (single_choice doesn't exist)
        q3 = SurveyQuestion.objects.create(
            survey_template=template,
            text="Which department did you visit?",
            text_ar="ما هو القسم الذي زرته؟",
            question_type='multiple_choice',
            order=3,
            is_required=True,
            choices_json=[
                {"value": "emergency", "label": "Emergency", "label_ar": "الطوارئ"},
                {"value": "outpatient", "label": "Outpatient", "label_ar": "العيادات الخارجية"},
                {"value": "inpatient", "label": "Inpatient", "label_ar": "الإقامة الداخلية"},
                {"value": "surgery", "label": "Surgery", "label_ar": "الجراحة"},
                {"value": "radiology", "label": "Radiology", "label_ar": "الأشعة"}
            ]
        )
        self.stdout.write(f'  ✓ Question 3: Multiple Choice - {q3.text}')
        self.stdout.write(f'    Choices: {", ".join([c["label"] for c in q3.choices_json])}')

        # Question 4: Another multiple choice question
        q4 = SurveyQuestion.objects.create(
            survey_template=template,
            text="What aspects of your experience were satisfactory? (Select all that apply)",
            text_ar="ما هي جوانب تجربتك التي كانت مرضية؟ (حدد جميع ما ينطبق)",
            question_type='multiple_choice',
            order=4,
            is_required=False,
            choices_json=[
                {"value": "staff", "label": "Staff friendliness", "label_ar": "لطف الموظفين"},
                {"value": "cleanliness", "label": "Cleanliness", "label_ar": "النظافة"},
                {"value": "communication", "label": "Communication", "label_ar": "التواصل"},
                {"value": "wait_times", "label": "Wait times", "label_ar": "أوقات الانتظار"},
                {"value": "facilities", "label": "Facilities", "label_ar": "المرافق"}
            ]
        )
        self.stdout.write(f'  ✓ Question 4: Multiple Choice - {q4.text}')
        self.stdout.write(f'    Choices: {", ".join([c["label"] for c in q4.choices_json])}')

        # Question 5: Another rating question
        q5 = SurveyQuestion.objects.create(
            survey_template=template,
            text="How would you rate the hospital facilities?",
            text_ar="كيف تقي مرافق المستشفى؟",
            question_type='rating',
            order=5,
            is_required=True
        )
        self.stdout.write(f'  ✓ Question 5: Rating - {q5.text}')

        self.stdout.write(self.style.SUCCESS('\n' + '='*70))
        self.stdout.write(self.style.SUCCESS('DEMO SURVEY TEMPLATE CREATED SUCCESSFULLY!'))
        self.stdout.write(self.style.SUCCESS('='*70))
        self.stdout.write(f'\nTemplate ID: {template.id}')
        self.stdout.write(f'Template Name: {template.name}')
        self.stdout.write(f'Total Questions: {template.questions.count()}')
        
        self.stdout.write('\nQuestion Types Summary:')
        self.stdout.write(f'  - Text questions: {template.questions.filter(question_type="text").count()}')
        self.stdout.write(f'  - Rating questions: {template.questions.filter(question_type="rating").count()}')
        self.stdout.write(f'  - Single Choice questions: {template.questions.filter(question_type="single_choice").count()}')
        self.stdout.write(f'  - Multiple Choice questions: {template.questions.filter(question_type="multiple_choice").count()}')

        self.stdout.write(self.style.SUCCESS('\n' + '='*70))
        self.stdout.write(self.style.SUCCESS('NEXT STEPS:'))
        self.stdout.write(self.style.SUCCESS('='*70))
        self.stdout.write('1. Open your browser and go to: http://localhost:8000/surveys/templates/')
        self.stdout.write(f'2. Find and click on: {template.name}')
        self.stdout.write('3. You\'ll see the survey builder with all questions')
        self.stdout.write('4. The preview panel will show how each question type appears to patients')
        
        self.stdout.write('\nPreview Guide:')
        self.stdout.write('  ✓ Text: Shows as a textarea input')
        self.stdout.write('  ✓ Rating: Shows 5 radio buttons (Poor to Excellent)')
        self.stdout.write('  ✓ Single Choice: Shows radio buttons, only one can be selected')
        self.stdout.write('  ✓ Multiple Choice: Shows checkboxes, multiple can be selected')
        
        self.stdout.write('\nBilingual Support:')
        self.stdout.write('  - All questions have both English and Arabic text')
        self.stdout.write('  - Preview will show Arabic if you switch language')
        self.stdout.write(self.style.SUCCESS('='*70))
