<?php

namespace App\Http\Controllers\Admin\Crm;

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

class CustomerController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Customer::query()->with(['owner']);
        $query->when($request->name, fn($q, $v) => $q->where('name', 'like', "%{$v}%"));
        $query->when($request->phone, fn($q, $v) => $q->where('phone', 'like', "%{$v}%"));
        $query->when($request->status, fn($q, $v) => $q->where('status', $v));
        $query->when($request->owner_id, fn($q, $v) => $q->where('owner_id', $v));

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

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'lead_id' => 'nullable|exists:leads,id',
            'name' => 'required|string|max:50',
            'phone' => 'required|string|max:20',
            'id_card' => 'nullable|string|max:20',
            'wechat' => 'nullable|string|max:50',
            'birthday' => 'nullable|date',
            'expected_date' => 'nullable|date',
            'actual_date' => 'nullable|date',
            'baby_count' => 'sometimes|integer|min:1',
            'tags' => 'nullable|array',
            'remark' => 'nullable|string',
            'families' => 'nullable|array',
            'families.*.name' => 'nullable|string|max:50',
            'families.*.phone' => 'nullable|string|max:20',
            'families.*.relation' => 'nullable|string|max:20',
            'families.*.is_emergency' => 'nullable|boolean',
        ]);

        $families = $validated['families'] ?? [];
        unset($validated['families']);
        $validated['status'] = 1;

        $customer = Customer::create($validated);
        if ($families) {
            $customer->families()->createMany($families);
        }

        return $this->success($customer->load('families'));
    }

    public function show(Customer $customer): JsonResponse
    {
        return $this->success($customer->load(['owner', 'families', 'contracts', 'complaints']));
    }

    public function update(Request $request, Customer $customer): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'sometimes|string|max:50',
            'phone' => 'sometimes|string|max:20',
            'id_card' => 'nullable|string|max:20',
            'wechat' => 'nullable|string|max:50',
            'birthday' => 'nullable|date',
            'expected_date' => 'nullable|date',
            'actual_date' => 'nullable|date',
            'baby_count' => 'sometimes|integer|min:1',
            'tags' => 'nullable|array',
            'status' => 'sometimes|integer|in:1,2,3,4,5',
            'owner_id' => 'nullable|exists:users,id',
            'remark' => 'nullable|string',
        ]);
        $customer->update($validated);
        return $this->success($customer);
    }

    public function destroy(Customer $customer): JsonResponse
    {
        $customer->delete();
        return $this->success(null);
    }
}
