<?php

namespace App\Http\Controllers\Admin\Finance;

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

class InvoiceController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Invoice::query()->with(['customer']);
        $query->when($request->invoice_no, fn($q, $v) => $q->where('invoice_no', 'like', "%{$v}%"));
        $query->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
        $query->when($request->has('status'), fn($q) => $q->where('status', $request->status));
        $query->when($request->type, fn($q, $v) => $q->where('type', $v));

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

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'invoice_no' => 'required|string|max:50|unique:invoices',
            'customer_id' => 'nullable|exists:customers,id',
            'type' => 'sometimes|integer|in:1,2',
            'amount' => 'required|numeric|min:0',
            'tax_amount' => 'sometimes|numeric|min:0',
            'title' => 'nullable|string|max:255',
            'tax_no' => 'nullable|string|max:50',
            'related_type' => 'nullable|string|max:50',
            'related_id' => 'nullable|integer',
        ]);

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

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

    public function show(Invoice $invoice): JsonResponse
    {
        return $this->success($invoice->load(['customer', 'operator']));
    }

    public function update(Request $request, Invoice $invoice): JsonResponse
    {
        if ($invoice->status !== 0) {
            return $this->error('只能修改待开票的发票', 40001);
        }

        $validated = $request->validate([
            'customer_id' => 'nullable|exists:customers,id',
            'type' => 'sometimes|integer|in:1,2',
            'amount' => 'sometimes|numeric|min:0',
            'tax_amount' => 'sometimes|numeric|min:0',
            'title' => 'nullable|string|max:255',
            'tax_no' => 'nullable|string|max:50',
            'related_type' => 'nullable|string|max:50',
            'related_id' => 'nullable|integer',
        ]);

        $invoice->update($validated);

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

    public function destroy(Invoice $invoice): JsonResponse
    {
        if ($invoice->status !== 0) {
            return $this->error('只能删除待开票的发票', 40001);
        }

        $invoice->delete();

        return $this->success(null);
    }

    public function issue(Invoice $invoice): JsonResponse
    {
        if ($invoice->status !== 0) {
            return $this->error('该发票已开具或已红冲', 40001);
        }

        $invoice->update([
            'status' => 1,
            'issued_at' => now(),
        ]);

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