package com.drinkool;

import com.drinkool.dtos.*;
import com.drinkool.models.Customer;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;

@Path("/api")
public class RestAPI {

	@POST
	@Path("/signup")
	@Transactional
	public Response signup(Signup input) {
		Customer newCustomer = new Customer();

		newCustomer.signup(input.name, input.phone, input.password);

		return Response.status(Response.Status.CREATED).build();
	}

	@POST
	@Path("/quickSignup")
	@Transactional
	public Response quickSignup(QuickSignup input) {
		Customer newCustomer = new Customer();

		newCustomer.signup(input.phone);

		return Response.status(Response.Status.CREATED).build();
	}

	@POST
	@Path("/login")
	public Response login(Login input) {
		Customer customer = Customer.find("phone", input.phone).firstResult();

		if (customer == null) throw new BadRequestException("InvalidPhoneNumber");

		if (!customer.login(input.password)) throw new BadRequestException(
			"InvalidPassword"
		);

		return Response.accepted().build();
	}
}
