<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreCustomerRequest;
use App\Models\Customer;
use Illuminate\Support\Facades\Cache;
class CustomerApiController extends Controller
{
    public function store(StoreCustomerRequest $request)
    {
        $user = auth()->user();

        if ($request->is_dealer_type) {
            return response()->json([
                'success' => false,
                'message' => 'Only Farmer type customers can be created via this endpoint',
            ], 400);
        }

        Customer::create(
            [
                'type_id' => $request->type,
                'name' => $request->name,
                'owner_name' => $request->owner_name,
                'phone_number' => $request->phone_number,
                'address' => $request->address,
                'land_area' => $request->land_area,
                'type_crop' => $request->type_crop,
                'created_by' => $user->id
            ]
        );

        Cache::forget('customers');

        return response()->json([
            'success' => true,
            'message' => 'Customer created successfully',
        ], 201);
    }

}
