from django.db import migrations
from django.utils import timezone


def seed_trustforge_demo(apps, schema_editor):
    Business = apps.get_model('core', 'Business')
    Customer = apps.get_model('core', 'Customer')
    Job = apps.get_model('core', 'Job')
    ProofCard = apps.get_model('core', 'ProofCard')
    ReviewRequest = apps.get_model('core', 'ReviewRequest')
    Feedback = apps.get_model('core', 'Feedback')

    business, _ = Business.objects.get_or_create(
        slug='summit-home-services',
        defaults={
            'name': 'Summit Home Services',
            'industry': 'Roofing & Exterior',
            'primary_city': 'Austin',
            'primary_state': 'TX',
            'google_review_url': 'https://example.com/google-review',
            'is_active': True,
        },
    )
    customer, _ = Customer.objects.get_or_create(
        business=business,
        full_name='Avery Johnson',
        defaults={
            'email': 'avery@example.com',
            'phone': '(512) 555-0148',
            'city': 'Austin',
            'state': 'TX',
        },
    )
    job, _ = Job.objects.get_or_create(
        business=business,
        customer=customer,
        service_type='Roof replacement',
        defaults={
            'description': 'Replaced weather-damaged shingles, sealed flashing, and completed a same-day cleanup.',
            'technician_name': 'Luis Ramirez',
            'city': 'Austin',
            'state': 'TX',
            'completed_at': timezone.localdate(),
            'project_value': '14250.00',
            'status': 'proof_ready',
            'is_verified': True,
        },
    )
    proof_card, _ = ProofCard.objects.get_or_create(
        job=job,
        defaults={
            'customer_display_name': 'Verified homeowner',
            'is_anonymized': True,
            'testimonial_quote': 'The crew communicated clearly, protected the landscaping, and finished the roof beautifully in one day.',
            'rating': 5,
            'status': 'published',
            'is_featured': True,
            'attached_widget_label': 'Homepage proof gallery',
            'attached_pages': 'Homepage, Roofing service page',
            'published_at': timezone.now(),
        },
    )
    review_request, _ = ReviewRequest.objects.get_or_create(
        job=job,
        defaults={
            'status': 'responded',
            'channel': 'email',
            'sent_at': timezone.now(),
            'reviewed_at': timezone.now(),
            'delivery_note': 'Seeded demo review request',
        },
    )
    Feedback.objects.get_or_create(
        review_request=review_request,
        defaults={
            'experience': 'great',
            'rating': 5,
            'testimonial': proof_card.testimonial_quote,
            'follow_up_required': False,
            'is_public_approved': True,
        },
    )


def remove_trustforge_demo(apps, schema_editor):
    Business = apps.get_model('core', 'Business')
    Business.objects.filter(slug='summit-home-services').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(seed_trustforge_demo, remove_trustforge_demo),
    ]
