package org.hso.ecommerce.components;

import org.hso.ecommerce.entities.booking.PaymentMethod;
import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.Timestamp;
import java.util.Optional;

@Component
public class AdminInitializer {

	@Autowired
	private final UserRepository userRepository = null;

	@PostConstruct
	public void init() {
		Optional<Integer> numberOfEmployees = userRepository.numberOfEmployees();
		if (numberOfEmployees.orElse(0) == 0) {
			// create first admin user
			User firstAdmin = new User();
			firstAdmin.created = new Timestamp(System.currentTimeMillis());
			firstAdmin.name = "admin";
			firstAdmin.defaultPayment = new PaymentMethod();
			firstAdmin.defaultPayment.creditCardNumber = ""; //set empty number
			firstAdmin.email = "admin";
			firstAdmin.isActive = true;
			firstAdmin.isEmployee = true;
			firstAdmin.setPassword("admin");
			userRepository.save(firstAdmin); //save to DB
		}
	}
}
