package com.yaoai.admin.page;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.yaoai.common.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin-api/module-pages")
@RequiredArgsConstructor
@SaCheckLogin(type = "admin")
public class AdminPageDataController {

    private final AdminPageDataService adminPageDataService;

    @GetMapping("/reports/rules")
    public Result<List<Map<String, Object>>> getReportRules() {
        return Result.ok(adminPageDataService.getReportRules());
    }

    @PutMapping("/reports/rules")
    public Result<List<Map<String, Object>>> saveReportRules(@RequestBody List<Map<String, Object>> rules) {
        return Result.ok(adminPageDataService.saveReportRules(rules));
    }

    @GetMapping("/{pageKey}")
    public Result<Map<String, Object>> getPageData(@PathVariable String pageKey) {
        return Result.ok(adminPageDataService.getPageData(pageKey));
    }

    @GetMapping("/{pageKey}/settings")
    public Result<Map<String, Object>> getPageSettings(@PathVariable String pageKey) {
        return Result.ok(adminPageDataService.getPageSettings(pageKey));
    }

    @PutMapping("/{pageKey}/settings")
    public Result<Map<String, Object>> savePageSettings(@PathVariable String pageKey, @RequestBody Map<String, Object> settings) {
        return Result.ok(adminPageDataService.savePageSettings(pageKey, settings));
    }

    @PostMapping("/{pageKey}/rows")
    public Result<Map<String, Object>> addRow(@PathVariable String pageKey, @RequestBody Map<String, Object> row) {
        return Result.ok(adminPageDataService.addRow(pageKey, row));
    }

    @PutMapping("/{pageKey}/rows/{rowIndex}")
    public Result<Map<String, Object>> updateRow(
            @PathVariable String pageKey,
            @PathVariable int rowIndex,
            @RequestBody Map<String, Object> row) {
        return Result.ok(adminPageDataService.updateRow(pageKey, rowIndex, row));
    }

    @DeleteMapping("/{pageKey}/rows/{rowIndex}")
    public Result<Map<String, Object>> deleteRow(@PathVariable String pageKey, @PathVariable int rowIndex) {
        return Result.ok(adminPageDataService.deleteRow(pageKey, rowIndex));
    }

    @PostMapping(value = "/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) {
        return Result.ok(adminPageDataService.saveFile(file));
    }

    @PostMapping("/{pageKey}/rows/{rowIndex}/process")
    public Result<Map<String, Object>> processRow(@PathVariable String pageKey, @PathVariable int rowIndex) {
        return Result.ok(adminPageDataService.processRow(pageKey, rowIndex));
    }
}
