using Library.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Supabase;
using static Supabase.Postgrest.Constants;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using static Supabase.Gotrue.Constants;

namespace Library
{
    public interface IUserService
    {
        Task<List<Users>> GetUserById(int id);
        Task<List<Users>> UpdateUserById(int id);
        Task<Supabase.Gotrue.Session> UserRegistrationWithEmailAndPassword(string email, string password);
        Task<List<Users>> RegisteredUserAddData(string name, DateTime date_of_birth, int id_gender);
        Task<Supabase.Gotrue.Session> UserAuthWithEmailAndPassword(string email, string password);
    }
    public class UserService : IUserService
    {
        private readonly Client _client;

        public UserService(Client client)
        {
            _client = client;
        }

        public async Task<List<Users>> GetUserById(int id)
        {
            var response = await _client.From<Users>().Filter("id", Operator.Equals, id).Get();
            if (response != null && response.Models != null)
            {
                return response.Models; 
            }

            return new List<Users>();
        }
        public async Task<List<Users>> UpdateUserById(int id)
        {
            var response = await _client.From<Users>().Where(x => x.Id == id).Set(x => x.Name,"Алексей").Update();
            if (response != null && response.Models != null)
            {
                return response.Models; 
            }

            return new List<Users>();
        }
        public async Task<Supabase.Gotrue.Session> UserRegistrationWithEmailAndPassword(string email, string password)
        {
            var session = await _client.Auth.SignUp(email, password);

            if (session != null)
            {
                return session; 
            }

            return new Supabase.Gotrue.Session();
        }
        public async Task<Supabase.Gotrue.Session> UserAuthWithEmailAndPassword(string email, string password)
        {
            var session = await _client.Auth.SignIn(email, password);

            if (session != null)
            {
                return session;
            }

            return new Supabase.Gotrue.Session();
        }
        public async Task<List<Users>> RegisteredUserAddData(string name, DateTime date_of_birth, int id_gender)
        {
            var session = await _client.Auth.SignIn("user@user.ru", "user12345");
            var user = new Users
            {
                Name = name,
                Date_of_birth = date_of_birth,
                Id_gender = id_gender,
                UID = _client.Auth.CurrentUser.Id
            };
            var response = await _client.From<Users>().Insert(user);

            if (response != null && response.Models != null)
            {
                return response.Models; 
            }

            return new List<Users>();
        }
    }
}
