import pytest
from django.contrib.auth import get_user_model
from datetime import date
from apps.asset_management.models import Asset, AssetCategory, AssetLocation, AssetAssignment, Maintenance, Procurement, Vendor


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


@pytest.fixture
def category(db):
    return AssetCategory.objects.create(name="Electronics")


@pytest.fixture
def location(db):
    return AssetLocation.objects.create(name="Office A")


@pytest.fixture
def vendor(db):
    return Vendor.objects.create(name="Tech Supplier Inc")


@pytest.mark.django_db
class TestVendorModel:
    def test_create_vendor(self):
        vendor = Vendor.objects.create(
            name="Office Supplies Co",
            email="contact@officesupplies.com"
        )
        assert vendor.name == "Office Supplies Co"


@pytest.mark.django_db
class TestAssetCategoryModel:
    def test_create_category(self):
        cat = AssetCategory.objects.create(name="Furniture")
        assert cat.slug == "furniture"


@pytest.mark.django_db
class TestAssetLocationModel:
    def test_create_location(self):
        loc = AssetLocation.objects.create(name="Warehouse B")
        assert loc.slug == "warehouse-b"


@pytest.mark.django_db
class TestAssetModel:
    def test_create_asset(self, category, location):
        asset = Asset.objects.create(
            name="MacBook Pro",
            asset_tag="LAPTOP-001",
            category=category,
            location=location
        )
        assert asset.name == "MacBook Pro"
        assert asset.status == "available"

    def test_asset_unique_tag(self, category, location):
        Asset.objects.create(name="Asset 1", asset_tag="TAG-001", category=category)
        with pytest.raises(Exception):
            Asset.objects.create(name="Asset 2", asset_tag="TAG-001", category=category)


@pytest.mark.django_db
class TestAssetAssignmentModel:
    def test_create_assignment(self, category, location, user):
        from apps.hr.models import Employee
        # The hr post_save signal auto-creates an Employee per user — reuse it.
        employee = Employee.objects.get(user=user)
        asset = Asset.objects.create(
            name="Monitor",
            asset_tag="MON-001",
            category=category,
            location=location
        )
        assignment = AssetAssignment.objects.create(
            asset=asset,
            employee=employee,
            assigned_date=date.today()
        )
        assert assignment.status == "assigned"


@pytest.mark.django_db
class TestMaintenanceModel:
    def test_create_maintenance(self, category, location):
        asset = Asset.objects.create(name="Printer", asset_tag="PRINT-001", category=category)
        maint = Maintenance.objects.create(
            asset=asset,
            title="Monthly check",
            description="Regular maintenance",
            scheduled_date=date.today()
        )
        assert maint.status == "scheduled"


@pytest.mark.django_db
class TestProcurementModel:
    def test_create_procurement(self, user, vendor):
        proc = Procurement.objects.create(
            title="Order 50 laptops",
            requested_by=user,
            vendor=vendor
        )
        assert proc.status == "draft"