<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\Finance\FinanceRecord;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FinanceRecordController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = FinanceRecord::query()->with(['category']);
        $query->when($request->type, fn($q, $v) => $q->where('type', $v));
        $query->when($request->category_id, fn($q, $v) => $q->where('category_id', $v));
        $query->when($request->has('audit_status'), fn($q) => $q->where('audit_status', $request->audit_status));
        $query->when($request->start_date, fn($q, $v) => $q->where('record_date', '>=', $v));
        $query->when($request->end_date, fn($q, $v) => $q->where('record_date', '<=', $v));

        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'record_no' => 'required|string|max:50|unique:finance_records',
            'category_id' => 'nullable|exists:finance_categories,id',
            'type' => 'required|integer|in:1,2',
            'amount' => 'required|numeric|min:0.01',
            'pay_method' => 'nullable|integer|in:1,2,3,4',
            'description' => 'nullable|string|max:255',
            'record_date' => 'required|date',
        ]);

        $validated['operator_id'] = auth()->id();
        $validated['audit_status'] = 0;

        return $this->success(FinanceRecord::create($validated));
    }

    public function show(FinanceRecord $financeRecord): JsonResponse
    {
        return $this->success($financeRecord->load(['category', 'auditor', 'operator']));
    }

    public function update(Request $request, FinanceRecord $financeRecord): JsonResponse
    {
        if ($financeRecord->audit_status !== 0) {
            return $this->error('只能修改待审核的记录', 40001);
        }

        $validated = $request->validate([
            'category_id' => 'nullable|exists:finance_categories,id',
            'type' => 'sometimes|integer|in:1,2',
            'amount' => 'sometimes|numeric|min:0.01',
            'pay_method' => 'nullable|integer|in:1,2,3,4',
            'description' => 'nullable|string|max:255',
            'record_date' => 'sometimes|date',
        ]);

        $financeRecord->update($validated);

        return $this->success($financeRecord);
    }

    public function destroy(FinanceRecord $financeRecord): JsonResponse
    {
        if ($financeRecord->audit_status !== 0) {
            return $this->error('只能删除待审核的记录', 40001);
        }

        $financeRecord->delete();

        return $this->success(null);
    }

    public function audit(Request $request, FinanceRecord $financeRecord): JsonResponse
    {
        if ($financeRecord->audit_status !== 0) {
            return $this->error('该记录已审核', 40001);
        }

        $validated = $request->validate([
            'status' => 'required|integer|in:1,2',
        ]);

        $financeRecord->update([
            'audit_status' => $validated['status'],
            'audit_user_id' => auth()->id(),
            'audit_at' => now(),
        ]);

        return $this->success($financeRecord);
    }
}
