import uuid

import pytest

from app.db.session import SessionLocal
from app.models.branch import Branch
from test_products_inventory import headers, product, workspace

pytest_plugins = ("test_workspace",)


def test_unified_inventory_history_includes_adjustments_and_transfers(
    client,
    verified_user,
):
    data = workspace(client, verified_user, "Unified History")
    business = data["business"]["id"]
    source = data["accessible_branches"][0]["id"]

    db = SessionLocal()
    destination = Branch(
        business_id=uuid.UUID(business),
        name="Warehouse",
        is_active=True,
    )
    db.add(destination)
    db.commit()
    db.refresh(destination)
    destination_id = str(destination.id)
    db.close()

    created = product(
        client,
        verified_user,
        business,
        "Unified Product",
        sku="UH-1",
        barcode="987654321",
    ).json()

    adjustment = client.post(
        f"/api/businesses/{business}/branches/{source}/stock-adjustments",
        json={
            "product_id": created["id"],
            "quantity_changed": "10",
            "reason": "RESTOCK",
        },
        headers=headers(verified_user),
    )
    assert adjustment.status_code == 201

    transfer = client.post(
        f"/api/businesses/{business}/inventory-transfers",
        json={
            "source_branch_id": source,
            "destination_branch_id": destination_id,
            "product_id": created["id"],
            "quantity": "3",
        },
        headers=headers(verified_user),
    )
    assert transfer.status_code == 201

    response = client.get(
        f"/api/businesses/{business}/inventory/history",
        headers=headers(verified_user),
    )
    assert response.status_code == 200

    rows = [
        row for row in response.json()
        if row["product_id"] == created["id"]
    ]

    assert len(rows) == 3

    assert any(
        row["movement_type"] == "RESTOCK"
        and row["branch_id"] == source
        and row["quantity_changed"] == 10
        for row in rows
    )

    assert any(
        row["movement_type"] == "TRANSFER_OUT"
        and row["branch_id"] == source
        and row["quantity_changed"] == -3
        for row in rows
    )

    assert any(
        row["movement_type"] == "TRANSFER_IN"
        and row["branch_id"] == destination_id
        and row["quantity_changed"] == 3
        for row in rows
    )

    source_rows = client.get(
        f"/api/businesses/{business}/inventory/history",
        params={"branch_id": source},
        headers=headers(verified_user),
    )
    assert source_rows.status_code == 200
    assert all(row["branch_id"] == source for row in source_rows.json())

    transfer_in_rows = client.get(
        f"/api/businesses/{business}/inventory/history",
        params={"movement_type": "TRANSFER_IN"},
        headers=headers(verified_user),
    )
    assert transfer_in_rows.status_code == 200
    assert any(
        row["product_id"] == created["id"]
        and row["branch_id"] == destination_id
        for row in transfer_in_rows.json()
    )
