package com.example.wabi.domain.network

import android.util.Log
import com.example.wabi.model.responses.Response
import com.example.wabi.model.supabase.User
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.auth.auth
import io.github.jan.supabase.auth.providers.builtin.Email
import io.github.jan.supabase.auth.providers.builtin.OTP
import io.github.jan.supabase.postgrest.from
import jakarta.inject.Inject

class SupabaseServiceImpl @Inject constructor(private val supabase: SupabaseClient) :
    SupabaseService {
    override suspend fun signIn(email: String, password: String): Response {
        return try {
            var response = supabase.auth.signInWith(Email) {
                this.email = email
                this.password = password
            }
            Response(response.toString())
        } catch (e: Exception) {
            Log.d("error sign in", e.message.toString())
            Response("", e.message.toString())
        }
    }

    override suspend fun signOut(): Response {
        return try {
            var response = supabase.auth.signOut()
            Response(response.toString())
        } catch (e: Exception) {
            Log.d("error sign out", e.message.toString())
            Response("", e.message.toString())
        }
    }

    override suspend fun signUp(email: String) {
        TODO("Not yet implemented")
    }

    override suspend fun verifyEmail(email: String) {
        TODO("Not yet implemented")
    }

    override suspend fun updateUserData(
        email: String, password: String, nickname: String, theme: Int, imgeLink: String?
    ) {
        TODO("Not yet implemented")
    }

    //проверка того, вошёл ли пользователь в систему
    override suspend fun isSession(): Boolean {
        return supabase.auth.currentUserOrNull() != null
    }

    override suspend fun resetPassword(email: String): Response {
        return try {
            var response = supabase.auth.resetPasswordForEmail(email)
            Response(response.toString())
        } catch (e: Exception) {
            Log.d("reset password", e.message.toString())
            Response("", e.message.toString())
        }
    }

    override suspend fun getThemeOutBase(): Int {
        return try {
            val user = supabase.auth.currentUserOrNull()
            if (user != null) {
                val userData = supabase.from("user").select {
                    filter {
                        eq("id", user.id)
                    }
                }.decodeSingle<User>()
                userData.theme
            } else {
                -1
            }
        } catch (e: Exception) {
            Log.d("get theme", e.message.toString())
            -1
        }
    }

    override suspend fun singInWithOTP(meEmail: String) {
        try {
            return supabase.auth.signInWith(OTP) {
                email = meEmail
            }
        } catch (ex: Exception) {
            Log.e("Erros", ex.message.toString())
        }
    }
}