from django.test import TestCase

from buybackprogram.helpers import get_tracking_number

from .testdata.factories import ProgramFactory, UserIssuerFactory
from .testdata.load_eveuniverse import load_eveuniverse

CONTRACT_PRICES = {
    "total_all_items_raw": 1_000_000,
    "total_all_items": 950_000,
    "total_tax_amount": 50_000,
    "total_donation_amount": 0,
    "hauling_cost": 0,
    "total_hauling_cost": 0,
    "contract_net_total": 950_000,
}


class TestGetTrackingNumber(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        load_eveuniverse()

    def test_should_create_tracking_for_authenticated_user(self):
        user = UserIssuerFactory()
        program = ProgramFactory()
        tracking = get_tracking_number(user, program, None, [], CONTRACT_PRICES, "")
        self.assertEqual(tracking.program, program)
        self.assertEqual(tracking.issuer_user, user)
        self.assertIsNotNone(tracking.tracking_number)

    def test_should_create_tracking_for_anonymous_user(self):
        program = ProgramFactory()
        tracking = get_tracking_number(None, program, None, [], CONTRACT_PRICES, "")
        self.assertEqual(tracking.program, program)
        self.assertIsNone(tracking.issuer_user)
        self.assertIsNotNone(tracking.tracking_number)

    def test_tracking_number_is_unique_across_calls(self):
        user = UserIssuerFactory()
        program = ProgramFactory()
        t1 = get_tracking_number(user, program, None, [], CONTRACT_PRICES, "")
        t2 = get_tracking_number(user, program, None, [], CONTRACT_PRICES, "")
        self.assertNotEqual(t1.tracking_number, t2.tracking_number)

    def test_tracking_number_uses_configured_prefill(self):
        from buybackprogram.app_settings import BUYBACKPROGRAM_TRACKING_PREFILL

        user = UserIssuerFactory()
        program = ProgramFactory()
        tracking = get_tracking_number(user, program, None, [], CONTRACT_PRICES, "")
        self.assertTrue(
            tracking.tracking_number.startswith(BUYBACKPROGRAM_TRACKING_PREFILL)
        )

    def test_additional_notes_stored_on_tracking(self):
        user = UserIssuerFactory()
        program = ProgramFactory()
        note = "Please check item quality"
        tracking = get_tracking_number(user, program, None, [], CONTRACT_PRICES, note)
        self.assertEqual(tracking.additional_notes, note)

    def test_net_price_stored_correctly(self):
        user = UserIssuerFactory()
        program = ProgramFactory()
        tracking = get_tracking_number(user, program, None, [], CONTRACT_PRICES, "")
        self.assertEqual(int(tracking.net_price), CONTRACT_PRICES["contract_net_total"])
