import pytest
from fastapi import HTTPException

from api.v1.modules.crm.leads import service
from api.v1.modules.crm.leads.dto import LeadConvert, LeadCreate
from api.v1.modules.crm.pipelines import service as pipelines_service
from api.v1.modules.crm.pipelines.dto import PipelineCreate, StageCreate

T, C = 1, 1


def test_convert_rejects_invalid_stage(db):
    lead = service.create_lead(db, LeadCreate(name="P", company_name="Empresa"), T, C)
    with pytest.raises(HTTPException) as exc:
        service.convert_lead(db, lead.id, LeadConvert(create_opportunity=True, stage_id=999), T, C)
    assert exc.value.status_code == 422


def test_create_lead_defaults_to_new(db):
    lead = service.create_lead(db, LeadCreate(name="Prospecto X", company_name="XYZ SA"), T, C)
    assert lead.status == "new"


def test_convert_lead_creates_account_contact_opportunity(db):
    pipeline = pipelines_service.create_pipeline(db, PipelineCreate(name="Ventas", is_default=True), T, C)
    stage = pipelines_service.create_stage(db, StageCreate(pipeline_id=pipeline.id, name="Prospecto"), T, C)

    lead = service.create_lead(
        db,
        LeadCreate(
            name="Oportunidad IMMEX",
            company_name="Maquiladora del Norte",
            contact_name="María Pérez",
            email="maria@example.com",
            estimated_value=50000,
        ),
        T, C,
    )
    result = service.convert_lead(
        db, lead.id, LeadConvert(create_opportunity=True, pipeline_id=pipeline.id, stage_id=stage.id), T, C
    )

    assert result["account_id"] is not None
    assert result["contact_id"] is not None
    assert result["opportunity_id"] is not None
    assert result["lead"].status == "converted"
    assert result["lead"].converted_account_id == result["account_id"]


def test_convert_is_idempotent(db):
    lead = service.create_lead(db, LeadCreate(name="P", company_name="Empresa"), T, C)
    first = service.convert_lead(db, lead.id, LeadConvert(create_opportunity=False), T, C)
    second = service.convert_lead(db, lead.id, LeadConvert(create_opportunity=False), T, C)
    assert first["account_id"] == second["account_id"]


def test_convert_splits_contact_name(db):
    lead = service.create_lead(db, LeadCreate(name="P", contact_name="Juan Carlos Ramírez"), T, C)
    result = service.convert_lead(db, lead.id, LeadConvert(create_opportunity=False), T, C)

    from api.v1.modules.crm.contacts import service as contacts_service

    contact = contacts_service.get_contact(db, result["contact_id"], T, C)
    assert contact.first_name == "Juan"
    assert contact.last_name == "Carlos Ramírez"
