from datetime import datetime

from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse
from django.utils import timezone

from .models import Event


class EventModelTests(TestCase):
    def test_slug_is_created_on_save(self):
        event = Event.objects.create(
            name='City Market',
            start=timezone.make_aware(datetime(2026, 4, 10, 10, 0)),
            end=timezone.make_aware(datetime(2026, 4, 10, 14, 0)),
        )
        self.assertTrue(event.slug.startswith('city-market-2026-04-10'))


class CalendarViewTests(TestCase):
    def setUp(self):
        Event.objects.create(
            name='Riverfront Market',
            location='Riverfront Pavilion',
            start=timezone.make_aware(datetime(2026, 4, 10, 10, 0)),
            end=timezone.make_aware(datetime(2026, 4, 10, 16, 0)),
            is_published=True,
        )

    def test_home_page_renders(self):
        response = self.client.get(reverse('home'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Show visitors exactly where your business will be on any day.')

    def test_staff_dashboard_requires_login(self):
        response = self.client.get(reverse('event_dashboard'))
        self.assertEqual(response.status_code, 302)
        self.assertIn(reverse('login'), response.url)

    def test_staff_dashboard_for_staff_user(self):
        staff = User.objects.create_user(username='staffer', password='pass12345', is_staff=True)
        client = Client()
        client.login(username='staffer', password='pass12345')
        response = client.get(reverse('event_dashboard'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Manage your public event calendar securely')


class EventDashboardMutationTests(TestCase):
    def setUp(self):
        self.staff = User.objects.create_user(username='staffer', password='pass12345', is_staff=True)
        self.event = Event.objects.create(
            name='Editable Market',
            location='Town Square',
            start=timezone.make_aware(datetime(2026, 4, 12, 9, 0)),
            end=timezone.make_aware(datetime(2026, 4, 12, 12, 0)),
            is_published=True,
        )

    def test_staff_can_open_edit_page(self):
        self.client.login(username='staffer', password='pass12345')
        response = self.client.get(reverse('event_edit', args=[self.event.slug]))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Save changes')

    def test_staff_can_update_event(self):
        self.client.login(username='staffer', password='pass12345')
        response = self.client.post(
            reverse('event_edit', args=[self.event.slug]),
            {
                'name': 'Updated Market',
                'location': 'Town Square',
                'start': '2026-04-12T09:00',
                'end': '2026-04-12T13:00',
                'event_url': 'https://example.com/event',
                'summary': 'Updated details',
                'is_published': 'on',
            },
        )
        self.assertRedirects(response, reverse('event_dashboard_detail', args=[self.event.slug]))
        self.event.refresh_from_db()
        self.assertEqual(self.event.name, 'Updated Market')
        self.assertEqual(self.event.summary, 'Updated details')

    def test_staff_can_delete_event(self):
        self.client.login(username='staffer', password='pass12345')
        response = self.client.post(reverse('event_delete', args=[self.event.slug]))
        self.assertRedirects(response, reverse('event_dashboard'))
        self.assertFalse(Event.objects.filter(pk=self.event.pk).exists())
