<?php

namespace App\Http\Controllers\Admin\Crm;

use App\Http\Controllers\Controller;
use App\Models\Crm\Contract;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ContractController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Contract::query()->with(['customer', 'auditor']);
        $query->when($request->contract_no, fn($q, $v) => $q->where('contract_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));

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

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'customer_id' => 'required|exists:customers,id',
            'contract_no' => 'required|string|max:50|unique:contracts',
            'package_name' => 'nullable|string|max:100',
            'total_amount' => 'required|numeric|min:0',
            'discount_amount' => 'sometimes|numeric|min:0',
            'actual_amount' => 'required|numeric|min:0',
            'days' => 'nullable|integer|min:1',
            'check_in_date' => 'nullable|date',
            'check_out_date' => 'nullable|date|after_or_equal:check_in_date',
            'remark' => 'nullable|string',
        ]);
        $validated['created_by'] = auth()->id();
        $validated['status'] = 0;

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

    public function show(Contract $contract): JsonResponse
    {
        return $this->success($contract->load(['customer', 'auditor', 'creator']));
    }

    public function update(Request $request, Contract $contract): JsonResponse
    {
        if ($contract->status !== 0) {
            return $this->error('只能修改待审核的合同', 40001);
        }
        $validated = $request->validate([
            'package_name' => 'nullable|string|max:100',
            'total_amount' => 'sometimes|numeric|min:0',
            'discount_amount' => 'sometimes|numeric|min:0',
            'actual_amount' => 'sometimes|numeric|min:0',
            'days' => 'nullable|integer|min:1',
            'check_in_date' => 'nullable|date',
            'check_out_date' => 'nullable|date|after_or_equal:check_in_date',
            'remark' => 'nullable|string',
        ]);
        $contract->update($validated);
        return $this->success($contract);
    }

    public function destroy(Contract $contract): JsonResponse
    {
        if ($contract->status !== 0) {
            return $this->error('只能删除待审核的合同', 40001);
        }
        $contract->delete();
        return $this->success(null);
    }

    public function audit(Request $request, Contract $contract): JsonResponse
    {
        if ($contract->status !== 0) {
            return $this->error('该合同已审核', 40001);
        }
        $validated = $request->validate([
            'status' => 'required|integer|in:1,2',
            'audit_remark' => 'nullable|string|max:255',
        ]);
        $contract->update([
            'status' => $validated['status'],
            'audit_user_id' => auth()->id(),
            'audit_at' => now(),
            'audit_remark' => $validated['audit_remark'] ?? null,
        ]);

        if ($validated['status'] === 1) {
            $contract->customer->update(['status' => 2]);
        }

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