"""Verifier for cal-006: Create Recurring Meeting."""

import json
from datetime import datetime
from pathlib import Path

import pytest


@pytest.fixture
def workspace(request):
    """Resolve the workspace path from the --workspace CLI flag."""
    return Path(request.config.getoption("--workspace"))


@pytest.fixture
def recurring(workspace):
    """Load and return the recurring.json contents."""
    path = workspace / "recurring.json"
    assert path.exists(), "recurring.json not found in workspace"
    with open(path) as f:
        return json.load(f)


def test_output_file_exists(workspace):
    """recurring.json must exist in the workspace."""
    assert (workspace / "recurring.json").exists()


def test_meeting_count(recurring):
    """There must be exactly 4 meeting instances."""
    meetings = recurring.get("meetings", [])
    assert len(meetings) == 4, f"Expected 4 meetings, got {len(meetings)}"


def test_all_mondays(recurring):
    """All meetings must fall on Mondays."""
    for m in recurring.get("meetings", []):
        dt = datetime.strptime(m["date"], "%Y-%m-%d")
        assert dt.weekday() == 0, f"Date {m['date']} is not a Monday"


def test_correct_dates(recurring):
    """Meetings must be on 2026-03-16, 23, 30, and 2026-04-06."""
    expected_dates = {"2026-03-16", "2026-03-23", "2026-03-30", "2026-04-06"}
    actual_dates = {m["date"] for m in recurring.get("meetings", [])}
    assert actual_dates == expected_dates, f"Expected {expected_dates}, got {actual_dates}"


def test_correct_times(recurring):
    """All meetings must be 10:00 - 11:00."""
    for m in recurring.get("meetings", []):
        assert m["start_time"] == "10:00", f"Wrong start time: {m['start_time']}"
        assert m["end_time"] == "11:00", f"Wrong end time: {m['end_time']}"
        assert m["duration_minutes"] == 60, f"Wrong duration: {m['duration_minutes']}"


def test_series_id(recurring):
    """All meetings must have the correct series_id."""
    for m in recurring.get("meetings", []):
        assert m.get("series_id") == "series-weekly-sync"


def test_unique_ids(recurring):
    """Each meeting instance must have a unique id."""
    ids = [m["id"] for m in recurring.get("meetings", [])]
    assert len(ids) == len(set(ids)), "Meeting ids are not unique"


def test_participants(recurring):
    """All meetings must have the correct participants."""
    expected = {"alice@example.com", "bob@example.com", "charlie@example.com"}
    for m in recurring.get("meetings", []):
        actual = set(m.get("participants", []))
        assert actual == expected, f"Wrong participants in {m['id']}: {actual}"
