package com.example.backend.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.backend.db.dao.UserRepo;
import com.example.backend.models.User;

import jakarta.annotation.PostConstruct;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
@RequestMapping("/users")
@CrossOrigin(origins = "http://localhost:4200/")
public class UserController {

    /*@RequestMapping("/users")
    public String helloUsers(){
        return "Hello users";
    }*/

    private List<User> allUsers;

    @PostConstruct
    private void init(){
        this.allUsers = new ArrayList<>();
        this.allUsers.add(new User("Petar", "Petrovic", "pera", "pera123"));
        this.allUsers.add(new User("Zika", "Zikic", "zika", "zika123"));
    }

   
    @GetMapping()
    public List<User> getAllUsers(){
        return this.allUsers;
    }

    @GetMapping("/{username}")
    public User getUser(@PathVariable String username){
        for(int i=0; i<this.allUsers.size(); i++){
            if(this.allUsers.get(i).getUsername().equals(username)){
                return this.allUsers.get(i);
            }
        }
        return null;
    }

    @PostMapping("/login")
    public User login(@RequestBody User entity) {
        return new UserRepo().login(entity);
    }
    
}
