package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.department.CreateDepartmentDTO;
import com.lesingle.creation.dto.department.UpdateDepartmentDTO;
import com.lesingle.creation.service.DepartmentService;
import com.lesingle.creation.vo.department.DepartmentVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@Tag(name = "部门管理", description = "部门 CRUD 和树形结构接口")
@RestController
@RequestMapping("/api/departments")
@RequiredArgsConstructor
public class DepartmentController {

    private final DepartmentService departmentService;

    @PostMapping
    @Operation(summary = "创建部门")
    @PreAuthorize("hasAuthority('department:create')")
    public Result<DepartmentVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Valid CreateDepartmentDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        DepartmentVO result = departmentService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "查询部门列表")
    @PreAuthorize("hasAuthority('department:read')")
    public Result<List<DepartmentVO>> list(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(required = false) Long parentId) {
        Long tenantId = userPrincipal.getTenantId();
        List<DepartmentVO> result = departmentService.list(tenantId, parentId);
        return Result.success(result);
    }

    @GetMapping("/tree")
    @Operation(summary = "查询部门树形结构")
    @PreAuthorize("hasAuthority('department:read')")
    public Result<List<DepartmentVO>> tree(
            @AuthenticationPrincipal UserPrincipal userPrincipal) {
        Long tenantId = userPrincipal.getTenantId();
        List<DepartmentVO> result = departmentService.tree(tenantId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取部门详情")
    @PreAuthorize("hasAuthority('department:read')")
    public Result<DepartmentVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        DepartmentVO result = departmentService.getDetail(id, tenantId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新部门")
    @PreAuthorize("hasAuthority('department:update')")
    public Result<DepartmentVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Valid UpdateDepartmentDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long modifierId = userPrincipal.getUserId();
        DepartmentVO result = departmentService.update(id, dto, tenantId, modifierId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门")
    @PreAuthorize("hasAuthority('department:delete')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        departmentService.delete(id, tenantId);
        return Result.success(null);
    }
}
