<?php

namespace App\Http\Controllers\Admin\Crm;

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

class LeadController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Lead::query()->with(['channel', '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->channel_id, fn($q, $v) => $q->where('channel_id', $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([
            'channel_id' => 'nullable|exists:channels,id',
            'name' => 'required|string|max:50',
            'phone' => 'required|string|max:20',
            'wechat' => 'nullable|string|max:50',
            'expected_date' => 'nullable|date',
            'source' => 'nullable|string|max:50',
            'remark' => 'nullable|string',
        ]);
        $validated['created_by'] = auth()->id();
        $validated['status'] = 1;

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

    public function show(Lead $lead): JsonResponse
    {
        return $this->success($lead->load(['channel', 'owner', 'follows.user']));
    }

    public function update(Request $request, Lead $lead): JsonResponse
    {
        $validated = $request->validate([
            'channel_id' => 'nullable|exists:channels,id',
            'name' => 'sometimes|string|max:50',
            'phone' => 'sometimes|string|max:20',
            'wechat' => 'nullable|string|max:50',
            'expected_date' => 'nullable|date',
            'source' => 'nullable|string|max:50',
            'status' => 'sometimes|integer|in:1,2,3,4',
            'remark' => 'nullable|string',
        ]);
        $lead->update($validated);
        return $this->success($lead);
    }

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

    public function follow(Request $request, Lead $lead): JsonResponse
    {
        $validated = $request->validate([
            'type' => 'nullable|integer|in:1,2,3,4',
            'content' => 'nullable|string',
            'next_follow_at' => 'nullable|date',
        ]);
        $validated['lead_id'] = $lead->id;
        $validated['user_id'] = auth()->id();

        $follow = LeadFollow::create($validated);
        $lead->update(['status' => 2]);

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

    public function assign(Request $request, Lead $lead): JsonResponse
    {
        $validated = $request->validate([
            'owner_id' => 'required|exists:users,id',
        ]);
        $lead->update($validated);
        return $this->success($lead);
    }
}
