package com.caliverse.admin.domain.api;

import com.caliverse.admin.domain.request.MenuRequest;
import com.caliverse.admin.domain.response.MenuResponse;
import com.caliverse.admin.domain.service.MenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "메뉴", description = "메뉴 배너 조회 및 관리 api")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/menu")
public class MenuController {

    private final MenuService menuService;

    // 조회
    @GetMapping("/banner/list")
    public ResponseEntity<MenuResponse> getList(
            @RequestParam Map<String, String> requestParam){

        return ResponseEntity.ok().body(menuService.getList(requestParam));
    }
    // 상세 조회
    @GetMapping("/banner/detail/{id}")
    public ResponseEntity<MenuResponse> getList(
            @PathVariable("id") Long id){

        return ResponseEntity.ok().body(menuService.getdetail(id));
    }
    @PostMapping("/image-upload")
    public ResponseEntity<MenuResponse> imageUpload(
            @RequestParam("file") MultipartFile file){
        return ResponseEntity.ok().body( menuService.imageUpload(file));
    }
    @GetMapping("/image-down")
    public ResponseEntity<Resource> imageDown(
            @RequestParam("file") String fileName){
        Resource resource = menuService.fileDown(fileName);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);

        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
    @GetMapping("/image-delete")
    public ResponseEntity<MenuResponse> imageDelete(
            @RequestParam("file") String fileName){
        return ResponseEntity.ok().body( menuService.imageDelete(fileName));
    }
    @PostMapping("/banner")
    public ResponseEntity<MenuResponse> postMenuBanner(
            @RequestBody MenuRequest menuRequest){

        return ResponseEntity.ok().body(menuService.postBanner(menuRequest));
    }
    @PutMapping("/banner/{id}")
    public ResponseEntity<MenuResponse> updateMenuBanner(
            @PathVariable("id")Long id, @RequestBody MenuRequest menuRequest){

        return ResponseEntity.ok().body(menuService.updateBanner(id, menuRequest));
    }
    @DeleteMapping("/banner/delete")
    public ResponseEntity<MenuResponse> deleteMenuBanner(
            @RequestParam Long id){

        return ResponseEntity.ok().body(menuService.deleteBanner(id));
    }
}
