# -*- 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(account_password):
    return {
        'old_password': account_password,
        'new_password1': 'thisisnewpassword',
        'new_password2': 'thisisnewpassword',
    }


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

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

    AccountsTestingService().assert_password_changed(
        pk=account_with_password.pk,
        reference=account_with_password,
    )


@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.password'),
        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.password'),
        data=effective_payload,
    )

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

    assert 'old_password' in result.context['form'].errors
    assert 'new_password1' in result.context['form'].errors
    assert 'new_password2' 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.password'),
        data=effective_payload,
    )

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

    assert 'old_password' in result.context['form'].errors
    assert 'new_password1' in result.context['form'].errors
    assert 'new_password2' in result.context['form'].errors


@pytest.mark.django_db
def test_invaliid_bad_old_password(account_with_password_client: Client,
                                   payload,
                                   account_with_password,
                                   ):
    # Given
    payload['old_password'] = 'badpassword'

    # When
    result = account_with_password_client.post(
        reverse('ui.accounts.settings.password'),
        data=payload,
    )

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

    assert 'old_password' in result.context['form'].errors


@pytest.mark.django_db
def test_invaliid_bad_new_password1(account_with_password_client: Client,
                                    payload,
                                    account_with_password,
                                    ):
    # Given
    payload['new_password1'] = 'badpassword'

    # When
    result = account_with_password_client.post(
        reverse('ui.accounts.settings.password'),
        data=payload,
    )

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

    assert 'new_password2' in result.context['form'].errors


@pytest.mark.django_db
def test_invaliid_bad_new_password2(account_with_password_client: Client,
                                    payload,
                                    account_with_password,
                                    ):
    # Given
    payload['new_password2'] = 'badpassword'

    # When
    result = account_with_password_client.post(
        reverse('ui.accounts.settings.password'),
        data=payload,
    )

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

    assert 'new_password2' 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.password'),
    )

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


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

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