import pytest
from django.contrib.auth import get_user_model
from datetime import date, timedelta, datetime
from apps.administrations.models import (
    CarBooking, Reimbursement, PaymentRequest, DocumentRequest,
    LetterNumber, LetterPrefix, Announcement, SOP, InternalGuide
)


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


@pytest.mark.django_db
class TestCarBookingModel:
    def test_create_car_booking(self, user):
        booking = CarBooking.objects.create(
            requester=user,
            vehicle_name="Toyota Avanza",
            purpose="Client meeting",
            start_datetime=datetime.now(),
            end_datetime=datetime.now() + timedelta(hours=4),
            destination="Client Office"
        )
        assert booking.status == "pending"


@pytest.mark.django_db
class TestReimbursementModel:
    def test_create_reimbursement(self, user):
        reimb = Reimbursement.objects.create(
            requester=user,
            title="Travel Expenses",
            description="Flight and hotel",
            amount=1500000
        )
        assert reimb.status == "pending"


@pytest.mark.django_db
class TestPaymentRequestModel:
    def test_create_payment_request(self, user):
        pr = PaymentRequest.objects.create(
            requester=user,
            title="Vendor Payment",
            description="Pay vendor for services",
            amount=5000000,
            payee="Vendor XYZ"
        )
        assert pr.status == "pending"


@pytest.mark.django_db
class TestDocumentRequestModel:
    def test_create_document_request(self, user):
        doc_req = DocumentRequest.objects.create(
            requester=user,
            document_type="Tax Certificate",
            purpose="For visa application"
        )
        assert doc_req.status == "pending"


@pytest.mark.django_db
class TestLetterNumberModel:
    def test_create_letter_number(self):
        prefix = LetterPrefix.objects.create(code="SURAT", label="Surat Umum")
        letter = LetterNumber.objects.create(
            prefix=prefix,
            year=2026,
            month=4,
            sequence=1,
            subject="Meeting Minutes",
            recipient="All Staff",
            sender="Management",
            date=date.today()
        )
        # <seq 3-digit>/<prefix code>/<roman month>/<year>
        assert letter.full_number == "001/SURAT/IV/2026"


@pytest.mark.django_db
class TestAnnouncementModel:
    def test_create_announcement(self, user):
        ann = Announcement.objects.create(
            title="Office Closure",
            content="Office will be closed on Friday",
            author=user,
            valid_from=datetime.now()
        )
        assert ann.priority == "info"


@pytest.mark.django_db
class TestSOPModel:
    def test_create_sop(self, user):
        sop = SOP.objects.create(
            code="SOP-HR-001",
            title="Leave Policy",
            category="HR",
            content="Leave policy details...",
            effective_date=date.today(),
            author=user
        )
        assert sop.version == "1.0"


@pytest.mark.django_db
class TestInternalGuideModel:
    def test_create_internal_guide(self, user):
        guide = InternalGuide.objects.create(
            title="Onboarding Guide",
            category="HR",
            content="Welcome to the company...",
            author=user
        )
        assert guide.category == "HR"