import uuid
import pytest
from app.core.security import create_access_token

pytest_plugins=("test_workspace",)
def h(user):return {"Authorization":f"Bearer {create_access_token(str(user.id))}"}
def onboard(client,user,name):
    return client.post("/api/businesses/onboarding",json={"name":name,"main_branch_name":"Main","country":"Nigeria","currency":"NGN","default_language":"en"},headers=h(user)).json()["business"]["id"]
def test_customer_crud_search_deactivate_and_business_isolation(client,verified_user):
    business=onboard(client,verified_user,"Customer Store")
    created=client.post(f"/api/businesses/{business}/customers",json={"name":"Ada Buyer","phone":"0800","notes":"credit"},headers=h(verified_user));assert created.status_code==201
    customer=created.json();assert client.get(f"/api/businesses/{business}/customers?q=Ada",headers=h(verified_user)).json()[0]["id"]==customer["id"]
    updated=client.patch(f"/api/businesses/{business}/customers/{customer['id']}",json={"is_active":False},headers=h(verified_user));assert updated.status_code==200 and updated.json()["is_active"] is False
    other=client.post("/api/businesses",json={"name":"Other Store","main_branch_name":"Main","country":"Nigeria","currency":"NGN","default_language":"en"},headers={**h(verified_user),"Idempotency-Key":uuid.uuid4().hex}).json()["business"]["id"]
    assert client.get(f"/api/businesses/{other}/customers/{customer['id']}",headers=h(verified_user)).status_code==404
