using System;
using System.Collections.Generic;
using System.Linq;
using Demo.Domain.Models;
using Demo.Data.LocalData;

namespace Demo.Data.Repository
{
    public class UserRepositoryImpl : IUserRepository
    {
        public IEnumerable<User> GetAllUsers()
        {
            return LocalStaticData.Users.Select(u => new User
            {
                Id = u.Id,
                FIO = u.FIO,
                GroupID = u.GroupID
            });
        }

        public User GetUserById(Guid id)
        {
            var localUser = LocalStaticData.Users.FirstOrDefault(u => u.Id == id);
            if (localUser == null)
                throw new Exception("User not found");

            return new User
            {
                Id = localUser.Id,
                FIO = localUser.FIO,
                GroupID = localUser.GroupID
            };
        }

        public void AddUser(User user)
        {
            var localUser = new LocalData.Entity.User
            {
                Id = user.Id,
                FIO = user.FIO,
                GroupID = user.GroupID
            };
            LocalStaticData.Users.Add(localUser);
        }

        public void UpdateUser(User user)
        {
            var localUser = LocalStaticData.Users.FirstOrDefault(u => u.Id == user.Id);
            if (localUser == null)
                throw new Exception("User not found");

            localUser.FIO = user.FIO;
            localUser.GroupID = user.GroupID;
        }

        public void DeleteUser(Guid id)
        {
            var user = LocalStaticData.Users.FirstOrDefault(u => u.Id == id);
            if (user != null)
            {
                LocalStaticData.Users.Remove(user);
            }
        }
    }
}