package com.pressing.modules.accounting;

import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

@RestController
@RequestMapping("/api/v1/accounting")
@PreAuthorize("hasAnyRole('ADMIN','ACCOUNTANT','MANAGER')")
public class AccountingController {

    private final AccountingService accountingService;

    public AccountingController(AccountingService accountingService) {
        this.accountingService = accountingService;
    }

    @GetMapping("/entries")
    public Object entries() {
        return accountingService.entries();
    }

    @PostMapping("/entries")
    public Object create(@Valid @RequestBody AccountingService.EntryRequest req) {
        return accountingService.create(req);
    }

    @PutMapping("/entries/{id}")
    public Object update(@PathVariable Long id, @Valid @RequestBody AccountingService.EntryRequest req) {
        return accountingService.update(id, req);
    }

    @DeleteMapping("/entries/{id}")
    public void delete(@PathVariable Long id) {
        accountingService.delete(id);
    }

    @GetMapping("/reports")
    public Object reports() {
        return accountingService.reports();
    }

    @GetMapping("/settings")
    public Object settings() {
        return accountingService.settings();
    }

    @PostMapping("/settings")
    public Object createSetting(@Valid @RequestBody AccountingService.SettingRequest req) {
        return accountingService.createSetting(req);
    }

    @PutMapping("/settings/{id}")
    public Object updateSetting(@PathVariable Long id, @Valid @RequestBody AccountingService.SettingRequest req) {
        return accountingService.updateSetting(id, req);
    }
}
