package de.votian.services.controller;

import de.votian.services.entity.Address;
import de.votian.services.service.AddressService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/addresses")
public class AddressController {

    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @GetMapping("/{id}")
    public ResponseEntity<Address> findById(@PathVariable Long id) {
        return addressService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping("/find-or-create")
    public ResponseEntity<Address> findOrCreate(@RequestBody Address address) {
        return ResponseEntity.ok(addressService.findOrCreate(
                address.getStreet(), address.getZipcode(), address.getCity(), address.getCountry()));
    }

    @PostMapping
    public ResponseEntity<Address> save(@RequestBody Address address) {
        return ResponseEntity.ok(addressService.save(address));
    }
}
