from __future__ import annotations

import pytest
from django.contrib.auth.models import Group
from django.urls import reverse


@pytest.fixture
def cid_manager_user(db, django_user_model):
    user = django_user_model.objects.create_user(
        username="cid_manager",
        email="cid_manager@example.com",
        password="password123",
    )
    group, _ = Group.objects.get_or_create(name="cid_user_manager")
    user.groups.add(group)
    return user


@pytest.mark.django_db
def test_media_cleanup_requires_login(client):
    response = client.get(reverse("media_cleanup"))
    assert response.status_code == 302


@pytest.mark.django_db
def test_media_cleanup_requires_manager_group(client, django_user_model):
    user = django_user_model.objects.create_user(
        username="regular_user",
        email="regular@example.com",
        password="password123",
    )
    client.force_login(user)

    response = client.get(reverse("media_cleanup"))
    assert response.status_code == 403


@pytest.mark.django_db
def test_media_cleanup_page_for_manager(client, cid_manager_user):
    client.force_login(cid_manager_user)

    response = client.get(reverse("media_cleanup"))
    assert response.status_code == 200
    assert b"Unused Media Cleanup" in response.content
    assert b"Cleanup in progress" in response.content


@pytest.mark.django_db
def test_media_cleanup_preview_uses_dry_run(client, cid_manager_user, monkeypatch):
    client.force_login(cid_manager_user)

    captured = {}

    def fake_call_command(name, **kwargs):
        captured["name"] = name
        captured["kwargs"] = kwargs
        kwargs["stdout"].write("preview ok")

    monkeypatch.setattr("rad.views.call_command", fake_call_command)

    response = client.post(
        reverse("media_cleanup"),
        {
            "action": "preview",
            "minimum_file_age": "30",
            "remove_empty_dirs": "on",
            "exclude": "thumbnails/*\ncache/*",
        },
        HTTP_HX_REQUEST="true",
    )

    assert response.status_code == 200
    assert captured["name"] == "cleanup_unused_media"
    assert captured["kwargs"]["dry_run"] is True
    assert captured["kwargs"]["no_input"] is True
    assert captured["kwargs"]["interactive"] is False
    assert captured["kwargs"]["minimum_file_age"] == 30
    assert captured["kwargs"]["remove_empty_dirs"] is True
    assert captured["kwargs"]["exclude"] == ["thumbnails/*", "cache/*"]
    assert b"preview ok" in response.content


@pytest.mark.django_db
def test_media_cleanup_live_run_disables_dry_run(client, cid_manager_user, monkeypatch):
    client.force_login(cid_manager_user)

    captured = {}

    def fake_call_command(name, **kwargs):
        captured["name"] = name
        captured["kwargs"] = kwargs
        kwargs["stdout"].write("cleanup ok")

    monkeypatch.setattr("rad.views.call_command", fake_call_command)

    response = client.post(
        reverse("media_cleanup"),
        {
            "action": "cleanup",
            "minimum_file_age": "0",
        },
        HTTP_HX_REQUEST="true",
    )

    assert response.status_code == 200
    assert captured["name"] == "cleanup_unused_media"
    assert captured["kwargs"]["dry_run"] is False
    assert captured["kwargs"]["interactive"] is False
    assert b"cleanup ok" in response.content
    assert b"Recovered space" in response.content
    assert b"removed" in response.content
