package org.cmh.backend.NewsManagement.controller;

import jakarta.persistence.EntityNotFoundException;
import org.cmh.backend.NewsManagement.dto.GetNewsListResponse;
import org.cmh.backend.NewsManagement.dto.MessageResponse;
import org.cmh.backend.NewsManagement.dto.NewsRequest;
import org.cmh.backend.NewsManagement.dto.SearchNewsRequest;
import org.cmh.backend.NewsManagement.model.News;
import org.cmh.backend.NewsManagement.service.NewsService;
import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.Utils.JwtVerify;
import org.cmh.backend.authentication.model.UserHS;
import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private UserService userService;

    @GetMapping
    @JwtVerify
    public ResponseEntity<GetNewsListResponse> getNewsByRange(@RequestParam Integer start, @RequestParam Integer end, @RequestParam String token) {
        String username = JwtUtil.extractUsername(token);
        UserHS user = userService.getUserByUsername(username);
        // TODO:完善用户权限
        if (user.getSuperAdmin()) {

        } else {

        }

        if (start >= end) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        long newsCount = newsService.getNewsCount();
        return new ResponseEntity<>(new GetNewsListResponse(newsCount, newsService.getNewsByRange(start, end, user)), HttpStatus.OK);
    }

    @PostMapping("/search")
    @JwtVerify
    public ResponseEntity<GetNewsListResponse> searchNews(@RequestBody SearchNewsRequest request) {
        List<News> newsList = newsService.searchNews(request);
        long newsCount = newsList.size();
        return new ResponseEntity<>(new GetNewsListResponse(newsCount, newsList), HttpStatus.OK);
    }

    @GetMapping("/{id}")
    @JwtVerify
    public ResponseEntity<News> getNewsPage(@PathVariable Long id, @RequestParam String token) {
        try {
            return new ResponseEntity<>(newsService.getNewsById(id), HttpStatus.OK);
        } catch (EntityNotFoundException e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @PostMapping
    @JwtVerify
    public ResponseEntity<MessageResponse> createNews(@RequestBody NewsRequest request) {
        try {
            newsService.createNews(request);
        } catch (DataIntegrityViolationException e) {
            return new ResponseEntity<>(new MessageResponse("创建失败，文章已存在或缺少字段"), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(new MessageResponse("创建失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(new MessageResponse("创建成功"), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    @JwtVerify
    public ResponseEntity<MessageResponse> updateNews(@PathVariable Long id, @RequestBody NewsRequest request) {
        try {
            newsService.updateNews(id, request);
        } catch (DataIntegrityViolationException e) {
            return new ResponseEntity<>(new MessageResponse("修改失败，新标题已存在或缺少字段"), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(new MessageResponse("创建失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    @JwtVerify
    public ResponseEntity<MessageResponse> deleteNews(@PathVariable Long id, @RequestParam String token) {
        try {
            newsService.deleteNews(id);
        } catch (EntityNotFoundException e) {
            return new ResponseEntity<>(new MessageResponse("删除失败，文章不存在"), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
    }
}
