package com.vpnpanel.api;

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

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class ApiClient {
    
    private static final String BASE_URL = "https://your-domain.com/backend/api/";
    private static Retrofit retrofit = null;
    private static Context context;

    public static void init(Context ctx) {
        context = ctx.getApplicationContext();
    }

    public static Retrofit getClient() {
        if (retrofit == null) {
            // Logging Interceptor
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);

            // Auth Interceptor
            Interceptor authInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();
                    
                    // دریافت token از SharedPreferences
                    SharedPreferences prefs = context.getSharedPreferences("VPNPanel", Context.MODE_PRIVATE);
                    String token = prefs.getString("token", null);
                    
                    Request.Builder requestBuilder = original.newBuilder();
                    
                    if (token != null) {
                        requestBuilder.header("Authorization", "Bearer " + token);
                    }
                    
                    requestBuilder.header("Accept", "application/json");
                    requestBuilder.method(original.method(), original.body());
                    
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            };

            // OkHttp Client
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(logging)
                    .addInterceptor(authInterceptor)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .build();

            // Retrofit
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        
        return retrofit;
    }

    public static void clearToken() {
        if (context != null) {
            SharedPreferences prefs = context.getSharedPreferences("VPNPanel", Context.MODE_PRIVATE);
            prefs.edit().remove("token").apply();
        }
    }

    public static void saveToken(String token) {
        if (context != null) {
            SharedPreferences prefs = context.getSharedPreferences("VPNPanel", Context.MODE_PRIVATE);
            prefs.edit().putString("token", token).apply();
        }
    }

    public static String getToken() {
        if (context != null) {
            SharedPreferences prefs = context.getSharedPreferences("VPNPanel", Context.MODE_PRIVATE);
            return prefs.getString("token", null);
        }
        return null;
    }
}
