import csv
import tempfile
from datetime import datetime, timedelta
from pathlib import Path

import pytest

from rhodecode.lib.rc_commands.inactive_users import find_inactive_users, filter_reactivated_users
from rhodecode.model.db import User


@pytest.mark.usefixtures("app", "user_util")
class TestFilterInactiveUsers(object):
    def test_filter_inactive_users__user_with_email_never_logged_in(self, user_util):
        inactive_user = user_util.create_user(username="user_to_be_removed")
        user_util.create_user(username="normal_user")

        created_long_ago = datetime.now() - timedelta(days=300)
        inactive_user.created_on = created_long_ago
        user_id = inactive_user.user_id

        inactive_users_with_email, inactive_users_without_email = find_inactive_users()

        assert len(inactive_users_without_email) == 0
        assert len(inactive_users_with_email) == 1
        assert inactive_users_with_email[0].user_id == user_id

    def test_filter_inactive_users__user_without_email_never_logged_in(self, user_util):
        inactive_user = user_util.create_user(username="user_to_be_removed", email=None)
        user_util.create_user(username="normal_user")

        created_long_ago = datetime.now() - timedelta(days=300)
        inactive_user.created_on = created_long_ago
        user_id = inactive_user.user_id

        inactive_users_with_email, inactive_users_without_email = find_inactive_users()

        assert len(inactive_users_without_email) == 1
        assert len(inactive_users_with_email) == 0
        assert inactive_users_without_email[0].user_id == user_id

    def test_filter_inactive_users__user_with_email_last_activity_long_ago(self, user_util):
        inactive_user = user_util.create_user(username="user_to_be_removed")
        user_util.create_user(username="normal_user")

        activity_long_ago = datetime.now() - timedelta(days=300)
        inactive_user.last_activity = activity_long_ago
        user_id = inactive_user.user_id

        inactive_users_with_email, inactive_users_without_email = find_inactive_users()

        assert len(inactive_users_without_email) == 0
        assert len(inactive_users_with_email) == 1
        assert inactive_users_with_email[0].user_id == user_id

    def test_filter_inactive_users__user_without_email_last_activity_long_ago(self, user_util):
        inactive_user = user_util.create_user(username="user_to_be_removed", email=None)
        user_util.create_user(username="normal_user")

        activity_long_ago = datetime.now() - timedelta(days=300)
        inactive_user.last_activity = activity_long_ago
        user_id = inactive_user.user_id

        inactive_users_with_email, inactive_users_without_email = find_inactive_users()

        assert len(inactive_users_without_email) == 1
        assert len(inactive_users_with_email) == 0
        assert inactive_users_without_email[0].user_id == user_id

    def test_diff__previously_inactive_user_reactivated(self, user_util):
        prev_inactive_user = user_util.create_user(username="user_to_be_removed")
        activity_long_ago = datetime.now() - timedelta(days=300)
        prev_inactive_user.last_activity = activity_long_ago
        user_id = prev_inactive_user.user_id

        with tempfile.TemporaryDirectory() as td:
            path = Path(td) / "data.csv"
            with path.open("w", newline="", encoding="utf-8") as f:
                w = csv.writer(f)
                w.writerow(["user_id"])
                w.writerows([[user_id]])

            # reactivate user
            inactive_user = User.get(user_id=user_id)
            inactive_user.last_activity = datetime.now()

            inactive_users, reactivated_users = filter_reactivated_users(str(path))

            assert len(reactivated_users) == 1
            assert len(inactive_users) == 0
            assert reactivated_users[0].user_id == user_id

    def test_diff__previously_inactive_user_remain_inactive(self, user_util):
        prev_inactive_user = user_util.create_user(username="user_to_be_removed")
        activity_long_ago = datetime.now() - timedelta(days=300)
        prev_inactive_user.last_activity = activity_long_ago
        user_id = prev_inactive_user.user_id

        with tempfile.TemporaryDirectory() as td:
            path = Path(td) / "data.csv"
            with path.open("w", newline="", encoding="utf-8") as f:
                w = csv.writer(f)
                w.writerow(["user_id"])
                w.writerows([[user_id]])

            inactive_users, reactivated_users = filter_reactivated_users(str(path))

            assert len(reactivated_users) == 0
            assert len(inactive_users) == 1
            assert inactive_users[0].user_id == user_id
