import pytest
from django.contrib.auth import get_user_model
from datetime import date, timedelta
from decimal import Decimal
from apps.procurements.models import Requisition, Approval, RFQRFP, PurchaseOrder, ProcurementContract, GoodsReceipt, Invoice, Payment


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


@pytest.fixture
def vendor(db):
    from apps.asset_management.models import Vendor
    return Vendor.objects.create(name="Supplier Co")


@pytest.mark.django_db
class TestRequisitionModel:
    def test_create_requisition(self, user):
        req = Requisition.objects.create(
            title="Office supplies",
            requested_by=user
        )
        assert req.status == "draft"


@pytest.mark.django_db
class TestRFQRFPModel:
    def test_create_rfq(self, user):
        rfq = RFQRFP.objects.create(
            title="Request for Quote",
            rfq_rfp_type="rfq",
            description="Need 100 laptops",
            requirements="Specs here",
            deadline=date.today() + timedelta(days=7),
            created_by=user
        )
        assert rfq.rfq_rfp_type == "rfq"
        assert rfq.status == "draft"


@pytest.mark.django_db
class TestPurchaseOrderModel:
    def test_create_po(self, user, vendor):
        po = PurchaseOrder.objects.create(
            order_number="PO-001",
            vendor=vendor,
            subtotal=Decimal("1000.00"),
            tax=Decimal("100.00"),
            total=Decimal("1100.00")
        )
        assert po.order_number == "PO-001"
        assert po.status == "draft"


@pytest.mark.django_db
class TestProcurementContractModel:
    def test_create_contract(self, user, vendor):
        contract = ProcurementContract.objects.create(
            title="Annual Service Contract",
            contract_number="CON-001",
            vendor=vendor,
            value=Decimal("50000.00"),
            start_date=date.today()
        )
        assert contract.status == "active"


@pytest.mark.django_db
class TestGoodsReceiptModel:
    def test_create_receipt(self, user, vendor):
        from apps.procurements.models import PurchaseOrder
        po = PurchaseOrder.objects.create(
            order_number="PO-002",
            vendor=vendor,
            subtotal=Decimal("500.00"),
            tax=Decimal("50.00"),
            total=Decimal("550.00")
        )
        receipt = GoodsReceipt.objects.create(
            purchase_order=po,
            receipt_number="GR-001",
            received_date=date.today(),
            received_by=user
        )
        assert receipt.status == "pending"


@pytest.mark.django_db
class TestInvoiceModel:
    def test_create_invoice(self, vendor):
        invoice = Invoice.objects.create(
            invoice_number="INV-001",
            vendor=vendor,
            amount=Decimal("1000.00"),
            tax=Decimal("100.00"),
            total=Decimal("1100.00"),
            due_date=date.today() + timedelta(days=30)
        )
        assert invoice.status == "pending"


@pytest.mark.django_db
class TestPaymentModel:
    def test_create_payment(self, vendor, user):
        from apps.procurements.models import PurchaseOrder, Invoice
        po = PurchaseOrder.objects.create(
            order_number="PO-003",
            vendor=vendor,
            subtotal=Decimal("200.00"),
            tax=Decimal("20.00"),
            total=Decimal("220.00")
        )
        invoice = Invoice.objects.create(
            invoice_number="INV-002",
            vendor=vendor,
            amount=Decimal("200.00"),
            tax=Decimal("20.00"),
            total=Decimal("220.00"),
            due_date=date.today() + timedelta(days=30)
        )
        payment = Payment.objects.create(
            invoice=invoice,
            amount=Decimal("220.00"),
            method="bank_transfer"
        )
        assert payment.status == "pending"