# -*- 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 {
        'theme': 'cosmo',
        'light_mode': True,
        'auto_load_embeds': 'True',
    }


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

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

    AccountsTestingService().assert_settings_edited(
        pk=account.pk,
        update={
            'theme': 'cosmo',
            'light_mode': True,
            'auto_load_embeds': True,
        },
        reference=account,
    )


@pytest.mark.django_db
def test_invalid_invalid_selects(authenticated_client: Client):
    # Given
    effective_payload = {
        'auto_load_embeds': 'thisisntright',
    }

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

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

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


@pytest.mark.xfail(
    reason='WIP',
)
@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.settings'),
        data=effective_payload,
    )

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

    assert 'theme' in result.context['form'].errors
    assert 'auto_load_embeds' not in result.context['form'].errors


@pytest.mark.xfail(
    reason='WIP',
)
@pytest.mark.django_db
def test_invalid_all_missing(authenticated_client: Client):
    # Given
    effective_payload = {}

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

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

    assert 'theme' in result.context['form'].errors
    assert 'auto_load_embeds' not 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.settings'),
    )

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


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

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