import type { LoginData, RegisterData, ForgotPasswordData, ResetPasswordData } from '@/lib/types/auth';

const API_URL = process.env.NEXT_PUBLIC_API_URL;

if (!API_URL) {
    throw new Error("NEXT_PUBLIC_API_URL is not defined in .env file");
}

export const authService = {
    async login(data: LoginData) {
        const response = await fetch(`${API_URL}/auth/login`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(data),
        });
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "Login failed");
        }
        const result = await response.json();
        return result; // { access_token, user: { id, email, role }, expires_at }
    },
    async register(data: RegisterData): Promise<{ message: string, email: string }> {
        const response = await fetch(`${API_URL}/auth/register`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(data),
        });
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "Registration failed");
        }
        const result = await response.json();
        return result;
    },

    async forgotPassword(data: ForgotPasswordData): Promise<{ message: string, token: string }> {
        const response = await fetch(`${API_URL}/auth/forgot-password`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(data),
        });
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "Forgot password failed");
        }
        const result = await response.json();
        return result;
    },

    async resetPassword(data: ResetPasswordData): Promise<{ message: string }> {
        const response = await fetch(`${API_URL}/auth/reset-password`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(data),
        });
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "Reset password failed");
        }
        const result = await response.json();
        return result;
    },

    async verifyOtp(email: string, otp: string) {
        const response = await fetch(`${API_URL}/auth/verify-otp`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ email, otp }),
        });
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "OTP Verification failed");
        }
        return await response.json();
    },

    async resendOtp(email: string) {
        const response = await fetch(`${API_URL}/auth/resend-otp`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ email }),
        });
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "Resend OTP failed");
        }
        return await response.json();
    }
};