package com.hpay.hpay_mobile_api.controllers;

import com.hpay.hpay_mobile_api.DTO.ErrorResponse;
import com.hpay.hpay_mobile_api.DTO.SuccessResponse;
import com.hpay.hpay_mobile_api.entities.Ville;
import com.hpay.hpay_mobile_api.services.VilleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
@RequestMapping("/api/ville")
public class VilleController {

    @Autowired
    private VilleService villeService;

    // API pour récupérer les villes d'un pays
    @GetMapping("/pays/{idPays}")
    public Object getVillesByPays(@PathVariable("idPays") Integer paysId) {

        Object response = villeService.getVillesByPaysId(paysId);

        if (response instanceof SuccessResponse) {
            return ResponseEntity.status(201).body(response);  // User created successfully
        } else if (response instanceof ErrorResponse) {
            ErrorResponse errorResponse = (ErrorResponse) response;
            return ResponseEntity.status(errorResponse.getStatusCode()).body(errorResponse);  // Error response
        }
        return ResponseEntity.status(400).body(new ErrorResponse("Unknown error", 400)); // Default error response

    }

}