using Library;
using Library.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTests
{
    [TestClass]
    public class UsersTests
    {
        private readonly IUserService _userService;

        public UsersTests()
        {
            var url = "https://eykhnnlgofgxdzlixydt.supabase.co";
            var key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5a2hubmxnb2ZneGR6bGl4eWR0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MjkyMzQ4NzgsImV4cCI6MjA0NDgxMDg3OH0.gG_nBmJ1KjC3se3HHNoBJLN8UA-aTw7JJw9PIMwJoL0";

            // Создаем экземпляр SupabaseClientFactory и GenderService
            var factory = new SupabaseClientFactory(url, key);
            var client = factory.CreateClient();
            _userService = new UserService(client);
        }

        [TestMethod]
        public async Task GetUserByIdAsync_ReturnsUser_WhenDataExists()
        {
            int id = 1;
            var users = await _userService.GetUserById(id);

            Assert.IsNotNull(users);
            Assert.AreEqual(1, users.Count);
            Assert.AreEqual(users[0].Name, "Юлия");
            Assert.AreEqual(users[0].Date_of_birth, new DateTime(2004,12,02));
            Assert.AreEqual(users[0].Id_gender, 1);
            Assert.AreEqual(users[0].UID, "4584e154-96db-4fbf-a26a-83187c7ae636");
        }
        [TestMethod]
        public async Task UpdateUserByIdAsync_ReturnsUser_WhenDataExists()
        {
            int id = 2;
            var users = await _userService.UpdateUserById(id);

            Assert.IsNotNull(users);
            Assert.AreEqual(1, users.Count);
            Assert.AreEqual(users[0].Name, "Алексей");
            Assert.AreEqual(users[0].Date_of_birth, new DateTime(2000, 05, 14));
            Assert.AreEqual(users[0].Id_gender, 2);
            Assert.AreEqual(users[0].UID, "92c159f5-c368-4a44-a3f4-9d9d206be89a");
        }

        private static Random random = new Random();

        public static string RandomString(int length)
        {
            const string chars = "abcdefghigklmnopqrstuvwxyz";
            return new string(Enumerable.Repeat(chars, length)
                .Select(s => s[random.Next(s.Length)]).ToArray());
        }
        [TestMethod]
        public async Task UserRegistrationWithEmailAndPassword_ReturnsSession()
        {
            string email = RandomString(5) + "@mail.ru";
            string password = "user12345";
            var session = await _userService.UserRegistrationWithEmailAndPassword(email, password);

            Assert.IsNotNull(session);
            Assert.AreEqual(2, session.User.AppMetadata.Count);
            Assert.AreEqual(session.User.Email, email);
        }
        [TestMethod]
        public async Task UserAuthWithEmailAndPassword_ReturnsSession()
        {
            string email = "user@user.ru";
            string password = "user12345";
            var session = await _userService.UserAuthWithEmailAndPassword(email, password);

            Assert.IsNotNull(session);
            Assert.AreEqual(2, session.User.AppMetadata.Count);
            Assert.AreEqual(session.User.Email, email);
        }
        [TestMethod]
        public async Task RegisteredUserAddData_ReturnsUser()
        {
            string name = "User";
            DateTime date_of_birth = new DateTime(2000, 01, 02);
            int id_gender = 1;
            var user = await _userService.RegisteredUserAddData(name,date_of_birth,id_gender);
            Assert.IsNotNull(user);
            Assert.AreEqual(1, user.Count);
            Assert.AreEqual(user[0].Name, "User");
            Assert.AreEqual(user[0].Date_of_birth, new DateTime(2000, 01, 01));
            Assert.AreEqual(user[0].Id_gender, 1);
        }
    }
}
