// FileDummy Sample Java File
package com.example.sample;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Sample Java application covering common patterns:
 * classes, interfaces, generics, streams, and collections.
 */
public class UserService {

    private final Map<Long, User> users = new HashMap<>();
    private long nextId = 1;

    /**
     * Creates a new user.
     * @param name the user's name
     * @param email the user's email
     * @return the created user
     */
    public User createUser(String name, String email) {
        if (name == null || name.isBlank()) {
            throw new IllegalArgumentException("Name cannot be blank");
        }
        if (email == null || !email.contains("@")) {
            throw new IllegalArgumentException("Invalid email: " + email);
        }
        User user = new User(nextId++, name, email);
        users.put(user.getId(), user);
        return user;
    }

    public Optional<User> findById(long id) {
        return Optional.ofNullable(users.get(id));
    }

    public List<User> findByRole(Role role) {
        return users.values().stream()
                .filter(u -> u.getRole() == role)
                .sorted(Comparator.comparing(User::getName))
                .collect(Collectors.toList());
    }

    public List<User> findAll() {
        return new ArrayList<>(users.values());
    }

    public boolean deleteUser(long id) {
        return users.remove(id) != null;
    }

    // ── Nested classes ──────────────────────────
    public static class User {
        private final long id;
        private String name;
        private String email;
        private Role role;
        private final LocalDateTime createdAt;

        public User(long id, String name, String email) {
            this.id = id;
            this.name = name;
            this.email = email;
            this.role = Role.USER;
            this.createdAt = LocalDateTime.now();
        }

        public long getId() { return id; }
        public String getName() { return name; }
        public String getEmail() { return email; }
        public Role getRole() { return role; }
        public void setRole(Role role) { this.role = role; }

        @Override
        public String toString() {
            return String.format("User{id=%d, name='%s', email='%s', role=%s}", id, name, email, role);
        }
    }

    public enum Role {
        ADMIN, USER, GUEST
    }

    // ── Entry point ─────────────────────────────
    public static void main(String[] args) {
        UserService service = new UserService();
        service.createUser("Alice", "alice@example.com");
        service.createUser("Bob", "bob@example.com");

        service.findAll().forEach(System.out::println);
    }
}
