package com.writeoff.module.setting.invoiceprofile.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.setting.invoiceprofile.dto.CreateInvoiceProfileRequest;
import com.writeoff.module.setting.invoiceprofile.dto.UpdateInvoiceProfileRequest;
import com.writeoff.module.setting.invoiceprofile.model.InvoiceProfileInfo;
import com.writeoff.module.setting.invoiceprofile.service.InvoiceProfileService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping({"/api/invoice-profiles", "/api/invoice-heads"})
public class InvoiceProfileController {
    private final InvoiceProfileService invoiceProfileService;

    public InvoiceProfileController(InvoiceProfileService invoiceProfileService) {
        this.invoiceProfileService = invoiceProfileService;
    }

    @GetMapping
    @RequirePermission(value = "invoice.profile.read", dataScope = DataScopeType.TENANT, auditAction = "INVOICE_PROFILE_LIST")
    public ApiResponse<PageResult<InvoiceProfileInfo>> list() {
        return ApiResponse.success(invoiceProfileService.list());
    }

    @PostMapping
    @RequirePermission(value = "invoice.profile.manage", dataScope = DataScopeType.TENANT, auditAction = "INVOICE_PROFILE_CREATE")
    public ApiResponse<InvoiceProfileInfo> create(@RequestBody @Valid CreateInvoiceProfileRequest request) {
        return ApiResponse.success(invoiceProfileService.create(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "invoice.profile.manage", dataScope = DataScopeType.TENANT, auditAction = "INVOICE_PROFILE_UPDATE")
    public ApiResponse<InvoiceProfileInfo> update(@PathVariable("id") Long id,
                                                  @RequestBody @Valid UpdateInvoiceProfileRequest request) {
        return ApiResponse.success(invoiceProfileService.update(id, request));
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "invoice.profile.manage", dataScope = DataScopeType.TENANT, auditAction = "INVOICE_PROFILE_ENABLE")
    public ApiResponse<InvoiceProfileInfo> enable(@PathVariable("id") Long id) {
        return ApiResponse.success(invoiceProfileService.enable(id));
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "invoice.profile.manage", dataScope = DataScopeType.TENANT, auditAction = "INVOICE_PROFILE_DISABLE")
    public ApiResponse<InvoiceProfileInfo> disable(@PathVariable("id") Long id) {
        return ApiResponse.success(invoiceProfileService.disable(id));
    }
}
