from django.test import TestCase
from rest_framework.test import APIClient

from apps.accounts.models import Team, TeamMember, User
from apps.billing.models import CreditAccount


class AuthApiTests(TestCase):
    def test_register_creates_user_team_member_credit_account_and_token(self):
        client = APIClient()
        response = client.post(
            "/api/auth/register/",
            {
                "username": "new-owner",
                "password": "strong-password",
                "email": "owner@example.com",
                "team_name": "Launch Team",
            },
            format="json",
        )

        self.assertEqual(response.status_code, 201)
        self.assertIn("token", response.data)
        user = User.objects.get(username="new-owner")
        team = Team.objects.get(name="Launch Team")
        self.assertEqual(team.owner, user)
        self.assertTrue(TeamMember.objects.filter(team=team, user=user, role=TeamMember.Role.OWNER).exists())
        self.assertTrue(CreditAccount.objects.filter(team=team).exists())

