from io import BytesIO

from django.core.files.uploadedfile import SimpleUploadedFile
from PIL import Image

from apps.company.models import TeamMember

SETTINGS = "/api/v1/backoffice/settings/"
ABOUT = "/api/v1/backoffice/about/"
TEAM = "/api/v1/backoffice/team/"


def _png() -> SimpleUploadedFile:
    buffer = BytesIO()
    Image.new("RGB", (40, 30), "blue").save(buffer, "PNG")
    return SimpleUploadedFile("logo.png", buffer.getvalue(), content_type="image/png")


def test_update_settings_and_social_links(staff_api, api, content):
    links = [{"network": "linkedin", "url": "https://linkedin.com/company/ceder"}]
    response = staff_api.patch(
        SETTINGS, {"phone": "+237 600 00 00 00", "social_links": links}, format="json"
    )
    assert response.status_code == 200
    public = api.get("/api/v1/settings/").json()
    assert public["phone"] == "+237 600 00 00 00"
    assert [link["network"] for link in public["social_links"]] == ["linkedin"]


def test_invalid_settings_are_rejected(staff_api, content):
    response = staff_api.patch(SETTINGS, {"email": "pas-un-email"}, format="json")
    assert response.status_code == 400
    links = [{"network": "myspace", "url": "https://x.y"}]
    assert staff_api.patch(SETTINGS, {"social_links": links}, format="json").status_code == 400


def test_logo_upload_and_remove(staff_api, api, content):
    response = staff_api.post(f"{SETTINGS}logo/", {"image": _png()}, format="multipart")
    assert response.status_code == 200
    assert response.json()["logo"].endswith(".png")
    assert api.get("/api/v1/settings/").json()["logo"]
    assert staff_api.delete(f"{SETTINGS}logo/").json()["logo"] is None


def test_about_page_with_lists(staff_api, api, content):
    data = staff_api.get(ABOUT).json()
    assert len(data["values"]) == 4 and len(data["stats"]) == 4
    stats = [{"value": "15+", "label": "années d'expérience"}]
    response = staff_api.patch(ABOUT, {"title": "Qui sommes-nous", "stats": stats}, format="json")
    assert response.status_code == 200
    public = api.get("/api/v1/about/").json()
    assert public["page"]["title"] == "Qui sommes-nous"
    assert public["stats"] == stats
    assert len(public["values"]) == 4


def test_about_image(staff_api, content):
    response = staff_api.post(f"{ABOUT}image/", {"image": _png()}, format="multipart")
    assert response.json()["image"].endswith(".png")


def test_team_member_crud_and_order(staff_api, api, content):
    first = staff_api.post(TEAM, {"name": "Awa", "role": "CEO"}, format="json").json()
    second = staff_api.post(TEAM, {"name": "Bruno", "role": "Dev"}, format="json").json()
    assert second["order"] == 1
    staff_api.post(f"{TEAM}reorder/", {"ids": [second["id"], first["id"]]}, format="json")
    assert [m["name"] for m in api.get("/api/v1/about/").json()["team"]] == ["Bruno", "Awa"]
    staff_api.post(f"{TEAM}{first['id']}/image/", {"image": _png()}, format="multipart")
    assert TeamMember.objects.get(pk=first["id"]).photo
    assert staff_api.delete(f"{TEAM}{first['id']}/").status_code == 204


def test_require_staff(api, content):
    assert api.get(SETTINGS).status_code == 403
    assert api.patch(ABOUT, {"title": "x"}, format="json").status_code == 403
    assert api.get(TEAM).status_code == 403
