package com.example.myapplication.service;

import android.content.Context;
import android.content.SharedPreferences;

public class AuthService {
    public static boolean login(Context ctx, String username, String password) {
        if (username == null || password == null) return false;
        username = username.trim();
        password = password.trim();
        if (username.isEmpty() || password.isEmpty()) return false;

        // verify against repository
        com.example.myapplication.repository.UserRepository repo = new com.example.myapplication.repository.UserRepository(ctx);
        boolean valid = repo.verifyUserValidity(username, password);
        if (!valid) return false;

        SharedPreferences prefs = ctx.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
        prefs.edit()
                .putString("currentUser", username)
                .putBoolean("isLoggedIn", true)
                .apply();
        return true;
    }

    public static void logout(Context ctx) {
        SharedPreferences prefs = ctx.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
        prefs.edit()
                .remove("isLoggedIn")
                .remove("currentUser")
                .remove("searchKeyword")
                .remove("filterSubject")
                .remove("currentNoteId")
                .apply();
    }

    public static boolean isLoggedIn(Context ctx) {
        SharedPreferences prefs = ctx.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
        return prefs.getBoolean("isLoggedIn", false);
    }

    public static String getCurrentUser(Context ctx) {
        SharedPreferences prefs = ctx.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
        return prefs.getString("currentUser", "张三");
    }

    public static String validateRegistration(String username, String password, String confirm, String gradeName, Long majorId) {
        if (username == null || password == null || confirm == null || gradeName == null || majorId == null) return "请输入完整信息";
        username = username.trim();
        password = password.trim();
        confirm = confirm.trim();
        if (username.length() < 3 || username.length() > 20) {
            return "用户名长度应为3-20个字符";
        }
        if (password.length() < 6) {
            return "密码长度不能少于6位";
        }
        if (!password.equals(confirm)) {
            return "两次输入的密码不一致";
        }
        java.util.List<String> allowed = java.util.Arrays.asList("大一","大二","大三","大四");
        if (!allowed.contains(gradeName)) {
            return "请选择正确的年级";
        }
        return null; // null 表示校验通过
    }

    // Persist user to repository after validation
    public static boolean register(Context ctx, String username, String password, String gradeName, Long majorId) {
        com.example.myapplication.repository.UserRepository repo = new com.example.myapplication.repository.UserRepository(ctx);
        com.example.myapplication.domain.User u =
                new com.example.myapplication.domain.User(username, username, password, 0, "", 10, null, majorId);
        return repo.insertUserWithGradeText(u, gradeName);
    }
}