# Generated by Django 5.2 on 2025-10-01 18:21

from django.db import migrations
import uuid


def add_homepage_templates(apps, schema_editor):
    """Add Real Estate Research Analyst and Project Manager templates, and set homepage flags."""
    PersistentAgentTemplate = apps.get_model("api", "PersistentAgentTemplate")

    # Create Real Estate Research Analyst template
    PersistentAgentTemplate.objects.create(
        id=uuid.uuid4(),
        code="real-estate-research-analyst",
        display_name="Real Estate Research Analyst",
        tagline="Finds properties, pulls comps, and tracks market trends",
        description="An always-on pretrained worker that monitors real estate listings, researches comparable properties, analyzes market data, and compiles reports on property values and investment opportunities.",
        charter="""You are a Real Estate Research Analyst. Your job is to:

1. Monitor real estate listing sites for properties matching specified criteria
2. Research comparable sales and rental data for properties of interest
3. Track market trends, pricing changes, and neighborhood developments
4. Compile property analysis reports with key metrics and insights
5. Alert stakeholders about new listings or market opportunities

Always provide data-driven insights with sources cited. Format your reports clearly with property details, financial analysis, and actionable recommendations.""",
        base_schedule="0 9 * * *",  # Daily at 9 AM
        schedule_jitter_minutes=30,
        event_triggers=[],
        default_tools=["perplexity_search-perplexity-search-web"],
        recommended_contact_channel="email",
        category="Research",
        priority=5,
        is_active=True,
        show_on_homepage=True,
    )

    # Create Project Manager template
    PersistentAgentTemplate.objects.create(
        id=uuid.uuid4(),
        code="project-manager",
        display_name="Project Manager",
        tagline="Tracks milestones, manages blockers, and keeps teams aligned",
        description="An always-on pretrained worker that coordinates project activities, tracks progress against milestones, manages task dependencies, identifies blockers, and keeps stakeholders informed with status updates and reports.",
        charter="""You are a Project Manager. Your job is to:

1. Track project milestones and deliverables
2. Monitor task completion and identify blockers
3. Coordinate with team members to gather status updates
4. Send regular progress reports to stakeholders
5. Flag risks and suggest mitigation strategies
6. Maintain project documentation and meeting notes

Always be proactive about surfacing issues early. Keep communication clear, concise, and action-oriented. Focus on removing obstacles and keeping the team moving forward.""",
        base_schedule="0 10 * * 1-5",  # Weekdays at 10 AM
        schedule_jitter_minutes=15,
        event_triggers=[],
        default_tools=["google_sheets-read-rows", "google_sheets-add-single-row"],
        recommended_contact_channel="email",
        category="Team Ops",
        priority=3,
        is_active=True,
        show_on_homepage=True,
    )

    # Set show_on_homepage=True for Pipeline Whisperer (sales lead gen)
    try:
        pipeline_whisperer = PersistentAgentTemplate.objects.get(code="sales-pipeline-whisperer")
        pipeline_whisperer.show_on_homepage = True
        pipeline_whisperer.save(update_fields=["show_on_homepage"])
    except PersistentAgentTemplate.DoesNotExist:
        pass  # Template doesn't exist yet, skip


def reverse_homepage_templates(apps, schema_editor):
    """Remove the templates and homepage flags."""
    PersistentAgentTemplate = apps.get_model("api", "PersistentAgentTemplate")

    # Delete the new templates
    PersistentAgentTemplate.objects.filter(code__in=[
        "real-estate-research-analyst",
        "project-manager",
    ]).delete()

    # Unset homepage flag on Pipeline Whisperer
    PersistentAgentTemplate.objects.filter(
        code="sales-pipeline-whisperer"
    ).update(show_on_homepage=False)


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0148_add_show_on_homepage_field'),
    ]

    operations = [
        migrations.RunPython(add_homepage_templates, reverse_homepage_templates),
    ]
