# -*- 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


@pytest.mark.django_db
def test_authenticated_ok(authenticated_client: Client,
                          association_out,
                          ):
    # When
    result = authenticated_client.get(
        reverse('ui.associations.view', args=(association_out.pk,)),
    )

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

    asserts.assertTemplateUsed('ui/associations/view.html')
    assert result.context['association'].pk == association_out.pk
    assert hasattr(result.context['association'], 'target') is True
    assert result.context['association'].target.pk == association_out.target.pk
    assert result.context['show_controls'] is True
    assert 'share_url' in result.context
    assert result.context['is_share'] is False
    assert result.context['og_card_url'] is None

    expected_share_url = reverse(
        'ui.associations.view',
        args=(association_out.pk,),
        query=[('share', 'true')],
    )
    assert result.context['share_url'].endswith(expected_share_url)


@pytest.mark.django_db
def test_authenticated_archived(authenticated_client: Client,
                                archived_association_out,
                                ):
    # When
    result = authenticated_client.get(
        reverse('ui.associations.view', args=(archived_association_out.pk,)),
    )

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

    asserts.assertTemplateUsed('ui/associations/view.html')
    assert result.context['show_controls'] is False


@pytest.mark.django_db
def test_authenticated_deleted(authenticated_client: Client,
                               deleted_association_out,
                               ):
    # When
    result = authenticated_client.get(
        reverse('ui.associations.view', args=(deleted_association_out.pk,)),
    )

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


@pytest.mark.django_db
def test_authenticated_deleted_save(authenticated_client: Client,
                                    deleted_save_association_out,
                                    ):
    # When
    result = authenticated_client.get(
        reverse('ui.associations.view', args=(deleted_save_association_out.pk,)),
    )

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


@pytest.mark.django_db
def test_authenticated_not_found(authenticated_client: Client,
                                 null_uuid,
                                 ):
    # When
    result = authenticated_client.get(
        reverse('ui.associations.view', args=(null_uuid,)),
    )

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


@pytest.mark.django_db
def test_authenticated_other_account_association(authenticated_client: Client,
                                                 other_account_association_out,
                                                 ):
    # When
    result = authenticated_client.get(
        reverse('ui.associations.view', args=(other_account_association_out.pk,)),
    )

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


@pytest.mark.django_db
def test_authenticated_share_ok(authenticated_client: Client,
                                other_account_association_out,
                                ):
    # When
    result = authenticated_client.get(
        reverse(
            'ui.associations.view',
            args=(other_account_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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

    asserts.assertTemplateUsed('ui/associations/view.html')
    assert result.context['association'].pk == other_account_association_out.pk
    assert hasattr(result.context['association'], 'target') is True
    assert result.context['association'].target.pk == other_account_association_out.target.pk
    assert result.context['show_controls'] is False
    assert 'share_url' in result.context
    assert result.context['is_share'] is True
    assert result.context['og_card_url'] is not None

    expected_share_url = reverse(
        'ui.associations.view',
        args=(other_account_association_out.pk,),
        query=[('share', 'true')],
    )
    assert result.context['share_url'].endswith(expected_share_url)


@pytest.mark.django_db
def test_authenticated_share_owner_ok(authenticated_client: Client,
                                      association_out,
                                      ):
    # When
    result = authenticated_client.get(
        reverse(
            'ui.associations.view',
            args=(association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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

    asserts.assertTemplateUsed('ui/associations/view.html')
    assert result.context['show_controls'] is False


@pytest.mark.django_db
def test_authenticated_share_archived(authenticated_client: Client,
                                      other_account_archived_association_out,
                                      ):
    # When
    result = authenticated_client.get(
        reverse(
            'ui.associations.view',
            args=(other_account_archived_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_authenticated_share_deleted(authenticated_client: Client,
                                     other_account_deleted_association_out,
                                     ):
    # When
    result = authenticated_client.get(
        reverse(
            'ui.associations.view',
            args=(other_account_deleted_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_authenticated_share_deleted_save(authenticated_client: Client,
                                          other_account_deleted_save_association_out,
                                          ):
    # When
    result = authenticated_client.get(
        reverse(
            'ui.associations.view',
            args=(other_account_deleted_save_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_authenticated_share_not_found(authenticated_client: Client,
                                       null_uuid,
                                       ):
    # When
    result = authenticated_client.get(
        reverse(
            'ui.associations.view', args=(null_uuid,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_anonymous_share_ok(client: Client,
                            association_out,
                            ):
    # When
    result = client.get(
        reverse(
            'ui.associations.view',
            args=(association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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

    asserts.assertTemplateUsed('ui/associations/view.html')
    assert result.context['association'].pk == association_out.pk
    assert result.context['show_controls'] is False


@pytest.mark.django_db
def test_anonymous_share_archived(client: Client,
                                  archived_association_out,
                                  ):
    # When
    result = client.get(
        reverse(
            'ui.associations.view',
            args=(archived_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_anonymous_share_deleted(client: Client,
                                 deleted_association_out,
                                 ):
    # When
    result = client.get(
        reverse(
            'ui.associations.view',
            args=(deleted_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_anonymous_share_deleted_save(client: Client,
                                      deleted_save_association_out,
                                      ):
    # When
    result = client.get(
        reverse(
            'ui.associations.view',
            args=(deleted_save_association_out.pk,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_anonymous_share_not_found(client: Client,
                                   null_uuid,
                                   ):
    # When
    result = client.get(
        reverse(
            'ui.associations.view', args=(null_uuid,),
            query=[('share', 'true')],
        ),
    )

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


@pytest.mark.django_db
def test_inactive_account(inactive_account_client: Client,
                          association_out,
                          ):
    # When
    result = inactive_account_client.get(
        reverse('ui.associations.view', args=(association_out.pk,)),
    )

    # Then
    asserts.assertRedirects(
        result,
        reverse(
            'ui.accounts.login',
            query=[
                (
                    'next',
                    reverse('ui.associations.view', args=(association_out.pk,)),
                ),
            ],
        ),
        fetch_redirect_response=False,
    )


@pytest.mark.django_db
def test_anonymous(client: Client,
                   association_out,
                   ):
    # When
    result = client.get(
        reverse('ui.associations.view', args=(association_out.pk,)),
    )

    # Then
    asserts.assertRedirects(
        result,
        reverse(
            'ui.accounts.login',
            query=[
                (
                    'next',
                    reverse('ui.associations.view', args=(association_out.pk,)),
                ),
            ],
        ),
        fetch_redirect_response=False,
    )
