"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthService = void 0;
const database_js_1 = require("../config/database.js");
const env_js_1 = require("../config/env.js");
const bcryptjs_1 = __importDefault(require("bcryptjs"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
class AuthService {
    static async register(data) {
        try {
            const existingUser = await database_js_1.prisma.user.findFirst({
                where: {
                    OR: [{ email: data.email }, { username: data.username }],
                },
            });
            if (existingUser) {
                return {
                    success: false,
                    error: existingUser.email === data.email ? "Email already exists" : "Username already exists",
                };
            }
            const hashedPassword = await bcryptjs_1.default.hash(data.password, 12);
            const user = await database_js_1.prisma.user.create({
                data: {
                    username: data.username,
                    email: data.email,
                    password: hashedPassword,
                },
            });
            const token = this.generateToken({
                userId: user.id,
                username: user.username,
                email: user.email,
            });
            return {
                success: true,
                data: {
                    token,
                    user: {
                        id: user.id,
                        username: user.username,
                        email: user.email,
                    },
                },
            };
        }
        catch (error) {
            console.error("Registration error:", error);
            return {
                success: false,
                error: "Registration failed",
            };
        }
    }
    static async login(data) {
        try {
            const user = await database_js_1.prisma.user.findUnique({
                where: {
                    email: data.email,
                },
            });
            if (!user || !(await bcryptjs_1.default.compare(data.password, user.password))) {
                return {
                    success: false,
                    error: "Invalid email or password",
                };
            }
            const token = this.generateToken({
                userId: user.id,
                username: user.username,
                email: user.email,
            });
            return {
                success: true,
                data: {
                    token,
                    user: {
                        id: user.id,
                        username: user.username,
                        email: user.email,
                    },
                },
            };
        }
        catch (error) {
            console.error("Login error:", error);
            return {
                success: false,
                error: "Login failed",
            };
        }
    }
    static generateToken(payload) {
        return jsonwebtoken_1.default.sign(payload, env_js_1.env.JWT_SECRET, { expiresIn: "7d" });
    }
}
exports.AuthService = AuthService;
