from datetime import datetime

from app_new.extensions import db


def test_utc_datetime_is_displayed_in_college_timezone(app):
    with app.app_context():
        rendered = app.jinja_env.filters['datetime_fmt'](datetime(2026, 8, 14, 22, 30))
        assert rendered == '15/08/2026 00:30'


def test_general_intervention_does_not_use_an_unrelated_equipment(authenticated_client, app):
    response = authenticated_client.post('/interventions/new', data={
        'title': 'Intervention générale de test',
        'equipment_id': 'divers',
        'priority': 'normale',
        'intervention_type': 'administratif',
    })
    assert response.status_code == 302

    from app_new.core.models.maintenance import Intervention
    with app.app_context():
        intervention = Intervention.query.filter_by(title='Intervention générale de test').one()
        assert intervention.equipment_id is None
        db.session.delete(intervention)
        db.session.commit()


def test_training_crud_uses_real_model_fields(authenticated_client, app):
    response = authenticated_client.post('/training/new', data={
        'title': 'Formation reçue par mail',
        'start_date': '2026-09-10',
        'end_date': '2026-09-11',
        'location': 'DSDEN',
    })
    assert response.status_code == 302

    from app_new.core.models.planning import Training, TrainingParticipant
    with app.app_context():
        training = Training.query.filter_by(name='Formation reçue par mail').one()
        training_id = training.id

    assert authenticated_client.get(f'/training/{training_id}').status_code == 200
    assert authenticated_client.post(f'/training/{training_id}/register').status_code == 302

    with app.app_context():
        participant = TrainingParticipant.query.filter_by(training_id=training_id).one()
        assert participant.is_confirmed is True
        db.session.delete(participant)
        db.session.delete(db.session.get(Training, training_id))
        db.session.commit()


def test_previously_broken_fixed_pages_respond(authenticated_client):
    assert authenticated_client.get('/admin/room-types/new').status_code == 302
    assert authenticated_client.get('/planning/availability/leave/new').status_code == 200
    assert authenticated_client.get('/room-types/new').status_code == 200
    assert authenticated_client.get('/yeastar/config').status_code == 200
    assert authenticated_client.get('/yeastar/api/extensions').status_code == 503
