package com.petshop.backend.controller;

import com.petshop.backend.dto.pet.MyPetRequest;
import com.petshop.backend.dto.pet.MyPetResponse;
import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.PetService;
import com.petshop.backend.util.AuthenticationHelper;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/my-pets")
@PreAuthorize("isAuthenticated()")
public class MyPetController {

    private final PetService petService;
    private final UserRepository userRepository;

    public MyPetController(PetService petService, UserRepository userRepository) {
        this.petService = petService;
        this.userRepository = userRepository;
    }

    @GetMapping
    public ResponseEntity<List<MyPetResponse>> getMyPets(@RequestParam(required = false) String status) {
        return ResponseEntity.ok(petService.getMyPets(currentUserId(), status));
    }

    @PostMapping
    public ResponseEntity<MyPetResponse> createMyPet(@Valid @RequestBody MyPetRequest request) {
        return ResponseEntity.ok(petService.createMyPet(currentUserId(), request));
    }

    @PutMapping("/{id}")
    public ResponseEntity<MyPetResponse> updateMyPet(@PathVariable Long id, @Valid @RequestBody MyPetRequest request) {
        return ResponseEntity.ok(petService.updateMyPet(currentUserId(), id, request));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMyPet(@PathVariable Long id) {
        petService.deleteMyPet(currentUserId(), id);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/{id}/image")
    public ResponseEntity<?> uploadMyPetImage(@PathVariable Long id, @RequestParam("image") MultipartFile image) {
        try {
            return ResponseEntity.ok(petService.uploadMyPetImage(currentUserId(), id, image));
        } catch (IllegalArgumentException ex) {
            return ResponseEntity.badRequest().body(Map.of("message", ex.getMessage()));
        } catch (IOException ex) {
            return ResponseEntity.badRequest().body(Map.of("message", "Failed to upload pet image: " + ex.getMessage()));
        }
    }

    private Long currentUserId() {
        User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
        return user.getId();
    }
}
