import pytest
from django.contrib.auth import get_user_model
from django.utils import timezone
from datetime import timedelta
from apps.tools.models import URL, QRCode, ImageToolUsage, FileConversion


@pytest.fixture
def user(db):
    User = get_user_model()
    return User.objects.create_user(email="tools@example.com", password="pass123")


@pytest.mark.django_db
class TestURLModel:
    def test_create_url(self, user):
        url = URL.objects.create(
            original_url="https://example.com/very/long/url",
            created_by=user
        )
        assert url.short_code is not None
        assert len(url.short_code) == 6
        assert url.clicks == 0

    def test_url_short_code_unique(self, user):
        url1 = URL.objects.create(original_url="https://example.com/1", created_by=user)
        url2 = URL.objects.create(original_url="https://example.com/2", created_by=user)
        assert url1.short_code != url2.short_code

    def test_url_increment_clicks(self, user):
        url = URL.objects.create(original_url="https://example.com", created_by=user)
        initial_clicks = url.clicks
        url.increment_clicks()
        assert url.clicks == initial_clicks + 1
        assert url.last_clicked_at is not None

    def test_url_expiration(self, user):
        url = URL.objects.create(
            original_url="https://example.com",
            created_by=user,
            expires_at=timezone.now() - timedelta(hours=1)
        )
        assert url.is_expired() is True

    def test_url_not_expired(self, user):
        url = URL.objects.create(
            original_url="https://example.com",
            created_by=user,
            expires_at=timezone.now() + timedelta(hours=1)
        )
        assert url.is_expired() is False

    def test_url_no_expiration(self, user):
        url = URL.objects.create(original_url="https://example.com", created_by=user)
        assert url.is_expired() is False


@pytest.mark.django_db
class TestQRCodeModel:
    def test_create_qr_code(self, user):
        qr = QRCode.objects.create(
            content="https://example.com",
            created_by=user
        )
        assert qr.short_code is not None
        assert qr.format == 'png'
        assert qr.downloads == 0

    def test_qr_code_custom_size(self, user):
        qr = QRCode.objects.create(
            content="https://example.com",
            created_by=user,
            size=500
        )
        assert qr.size == 500


@pytest.mark.django_db
class TestImageToolUsageModel:
    def test_create_image_usage(self, user):
        usage = ImageToolUsage.objects.create(
            tool_name="compress",
            original_filename="image.png",
            original_size=1000000,
            result_size=500000,
            compression_ratio=0.5,
            created_by=user
        )
        assert usage.tool_name == "compress"
        assert usage.compression_ratio == 0.5


@pytest.mark.django_db
class TestFileConversionModel:
    def test_create_file_conversion(self, user):
        conversion = FileConversion.objects.create(
            from_format="jpg",
            to_format="png",
            original_filename="image.jpg",
            result_filename="image.png",
            original_size=1000000,
            result_size=800000,
            created_by=user
        )
        assert conversion.from_format == "jpg"
        assert conversion.to_format == "png"
