from __future__ import annotations

from datetime import datetime, timedelta

from app import create_app
from app.cli import seed_badges
from app.extensions import db
from app.models import TaskInstance, TaskTemplate, User
from app.services.badges import evaluate_task_badges
from app.services.monthly import archive_months_missing_up_to_previous


def seed_database() -> None:
    app = create_app()
    with app.app_context():
        db.drop_all()
        db.create_all()
        seed_badges()

        admin = User(
            name="Henri",
            email="mail@hnz.io",
            avatar_path="images/avatars/default.svg",
            is_admin=True,
            notification_task_due_enabled=True,
            notification_monthly_winner_enabled=True,
        )
        admin.set_password("putzliga123")

        anna = User(
            name="Anna",
            email="anna@putzliga.local",
            avatar_path="images/avatars/anna.svg",
            notification_task_due_enabled=True,
            notification_monthly_winner_enabled=True,
        )
        anna.set_password("putzliga123")
        ben = User(
            name="Ben",
            email="ben@putzliga.local",
            avatar_path="images/avatars/ben.svg",
            notification_task_due_enabled=True,
            notification_monthly_winner_enabled=False,
        )
        ben.set_password("putzliga123")

        db.session.add_all([admin, anna, ben])
        db.session.flush()

        templates = [
            TaskTemplate(
                title="Küche wischen",
                description="Arbeitsfläche, Herd und Tisch sauber machen.",
                default_points=12,
                default_assigned_user_id=anna.id,
                recurrence_interval_value=3,
                recurrence_interval_unit="days",
                active=True,
            ),
            TaskTemplate(
                title="Bad putzen",
                description="Waschbecken, Spiegel und Dusche reinigen.",
                default_points=20,
                default_assigned_user_id=ben.id,
                recurrence_interval_value=1,
                recurrence_interval_unit="weeks",
                active=True,
            ),
            TaskTemplate(
                title="Müll runterbringen",
                description="Restmüll, Papier und Bio entsorgen.",
                default_points=8,
                default_assigned_user_id=anna.id,
                recurrence_interval_value=1,
                recurrence_interval_unit="weeks",
                active=True,
            ),
            TaskTemplate(
                title="Fensterbank entstauben",
                description="Wohnzimmer und Flur mitnehmen.",
                default_points=6,
                default_assigned_user_id=ben.id,
                recurrence_interval_unit="none",
                active=True,
            ),
            TaskTemplate(
                title="Flur-Grundreinigung",
                description="Einmal alles: saugen, wischen, Schuhe sortieren.",
                default_points=28,
                default_assigned_user_id=ben.id,
                recurrence_interval_unit="none",
                active=True,
            ),
            TaskTemplate(
                title="Bettwäsche wechseln",
                description="Neue Bettwäsche aufziehen.",
                default_points=15,
                default_assigned_user_id=anna.id,
                recurrence_interval_value=1,
                recurrence_interval_unit="months",
                active=True,
            ),
        ]
        db.session.add_all(templates)
        db.session.flush()

        now = datetime.now()
        current_month_anchor = now.replace(day=5, hour=10, minute=0, second=0, microsecond=0)
        previous_month_anchor = (current_month_anchor.replace(day=1) - timedelta(days=3)).replace(day=10)

        instances = [
            TaskInstance(
                task_template_id=templates[0].id,
                title=templates[0].title,
                description=templates[0].description,
                assigned_user_id=anna.id,
                due_date=(now + timedelta(days=1)).date(),
                status="soon",
                points_awarded=12,
            ),
            TaskInstance(
                task_template_id=templates[1].id,
                title=templates[1].title,
                description=templates[1].description,
                assigned_user_id=ben.id,
                due_date=(now - timedelta(days=1)).date(),
                status="overdue",
                points_awarded=20,
            ),
            TaskInstance(
                task_template_id=templates[2].id,
                title=templates[2].title,
                description=templates[2].description,
                assigned_user_id=anna.id,
                due_date=(now + timedelta(days=4)).date(),
                status="open",
                points_awarded=8,
            ),
            TaskInstance(
                task_template_id=templates[3].id,
                title=templates[3].title,
                description=templates[3].description,
                assigned_user_id=ben.id,
                due_date=(now + timedelta(days=2)).date(),
                status="soon",
                points_awarded=6,
            ),
            TaskInstance(
                task_template_id=templates[4].id,
                title=templates[4].title,
                description=templates[4].description,
                assigned_user_id=ben.id,
                due_date=(now - timedelta(days=3)).date(),
                status="completed",
                completed_at=current_month_anchor - timedelta(days=2),
                completed_by_user_id=anna.id,
                points_awarded=28,
            ),
            TaskInstance(
                task_template_id=templates[5].id,
                title=templates[5].title,
                description=templates[5].description,
                assigned_user_id=anna.id,
                due_date=(now - timedelta(days=9)).date(),
                status="completed",
                completed_at=current_month_anchor - timedelta(days=2),
                completed_by_user_id=anna.id,
                points_awarded=15,
            ),
            TaskInstance(
                task_template_id=templates[1].id,
                title=templates[1].title,
                description=templates[1].description,
                assigned_user_id=anna.id,
                due_date=(previous_month_anchor - timedelta(days=1)).date(),
                status="completed",
                completed_at=previous_month_anchor,
                completed_by_user_id=ben.id,
                points_awarded=20,
            ),
            TaskInstance(
                task_template_id=templates[0].id,
                title=templates[0].title,
                description=templates[0].description,
                assigned_user_id=anna.id,
                due_date=(previous_month_anchor - timedelta(days=2)).date(),
                status="completed",
                completed_at=previous_month_anchor - timedelta(days=1),
                completed_by_user_id=anna.id,
                points_awarded=12,
            ),
            TaskInstance(
                task_template_id=templates[2].id,
                title=templates[2].title,
                description=templates[2].description,
                assigned_user_id=anna.id,
                due_date=(previous_month_anchor + timedelta(days=2)).date(),
                status="completed",
                completed_at=previous_month_anchor + timedelta(days=2),
                completed_by_user_id=anna.id,
                points_awarded=8,
            ),
        ]
        db.session.add_all(instances)
        db.session.commit()

        for user in (admin, anna, ben):
            evaluate_task_badges(user)
        archive_months_missing_up_to_previous()
        print("Seed-Daten geschrieben.")


if __name__ == "__main__":
    seed_database()
