# -*- coding: utf-8 -*-
# type: ignore
from __future__ import annotations

import http

from django.test import Client
from django.urls import reverse
import pytest
from pytest_django import asserts

from hotpocket_backend_testing.services.accounts import AccountsTestingService


@pytest.fixture
def payload():
    return {
        'first_name': 'Ziomek',
        'last_name': 'Dog',
        'email': 'woof@ziomek.dog',
    }


@pytest.mark.django_db
def test_ok(authenticated_client: Client,
            payload,
            account,
            ):
    # When
    result = authenticated_client.post(
        reverse('ui.accounts.settings.profile'),
        data=payload,
    )

    # Then
    asserts.assertRedirects(
        result,
        reverse('ui.accounts.settings.profile'),
        fetch_redirect_response=False,
    )

    AccountsTestingService().assert_edited(
        pk=account.pk,
        update=payload,
        reference=account,
    )


@pytest.mark.xfail(
    reason="Apparently I'm too dumb to properly fix a federated account, LOL",
)
@pytest.mark.django_db
def test_federated_account(federated_account_client: Client,
                           payload,
                           federated_account,
                           ):
    # When
    result = federated_account_client.post(
        reverse('ui.accounts.settings.profile'),
        data=payload,
    )

    # Then
    assert result.status_code == 500


@pytest.mark.django_db
def test_invalid_all_empty(authenticated_client: Client,
                           payload,
                           ):
    # Given
    effective_payload = {
        key: ''
        for key
        in payload.keys()
    }

    # When
    result = authenticated_client.post(
        reverse('ui.accounts.settings.profile'),
        data=effective_payload,
    )

    # Then
    assert result.status_code == http.HTTPStatus.OK

    assert 'first_name' in result.context['form'].errors
    assert 'last_name' in result.context['form'].errors
    assert 'email' in result.context['form'].errors


@pytest.mark.django_db
def test_invalid_all_missing(authenticated_client: Client):
    # Given
    effective_payload = {}

    # When
    result = authenticated_client.post(
        reverse('ui.accounts.settings.profile'),
        data=effective_payload,
    )

    # Then
    assert result.status_code == http.HTTPStatus.OK

    assert 'first_name' in result.context['form'].errors
    assert 'last_name' in result.context['form'].errors
    assert 'email' in result.context['form'].errors


@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client):
    # When
    result = inactive_account_client.post(
        reverse('ui.accounts.settings.profile'),
    )

    # Then
    asserts.assertRedirects(
        result,
        reverse(
            'ui.accounts.login',
            query=[('next', reverse('ui.accounts.settings.profile'))],
        ),
        fetch_redirect_response=False,
    )


@pytest.mark.django_db
def test_anonymous(client: Client):
    # When
    result = client.post(
        reverse('ui.accounts.settings.profile'),
    )

    # Then
    asserts.assertRedirects(
        result,
        reverse(
            'ui.accounts.login',
            query=[('next', reverse('ui.accounts.settings.profile'))],
        ),
        fetch_redirect_response=False,
    )
