package com.example.backend.controllers;

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

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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import jakarta.annotation.PostConstruct;

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

    private List<User> allUsers;


    // ne treba nam nista od metoda koje rade sa this.allUsers, jer sad radimo sa bazom
    @PostConstruct
    public void init(){
        this.allUsers = new ArrayList<>();
        /*this.allUsers.add(new User("pera", "peric", "p", "p1"));
        this.allUsers.add(new User("zika", "zikic", "z", "z1"));*/
    }

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

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

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