<?php

namespace App\Http\Controllers;

use App\Models\ContactSales;
use Illuminate\Http\Request;

class ContactSalesController extends Controller
{
    public function add(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'role' => 'required',
            'email' => 'required',
            'number_phone' => 'required',
            'status' => 'required',
        ]);

        $data = $request->all();

        $data['created_by'] = '@System';
        $data['status'] = 'New Request';

        $result = ContactSales::create($data);

        if ($result) {
            return response()->json(['status' => 'success', 'data' => $result, 'message' => 'Your message has been successfully received by our Sales team! Someone from our team will be in touch with you shortly.', 'code' => 200], 200);
        } else {
            return response()->json(['status' => 'failed', 'message' => 'Your message has been failed!', 'code' => 400], 400);
        }
    }

    public function edit($id)
    {
        if (!$id || (int) $id < 0 || $id == "") {
            return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
            die();
        }

        $result = ContactSales::find($id);

        if ($result) {
            return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
        } else {
            return response()->json(['status' => 'failed', 'message' => 'failed get data Contact Sales, please try again later!', 'code' => 400], 400);
        }
    }

    public function update(Request $request, $id)
    {
        if (!$id || (int) $id < 0 || $id == "") {
            return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
        }

        $data = ContactSales::find($id);


        if ($data) {
            $result = $data->update($request->all());
        } else {
            return response()->json(['status' => 'failed', 'message' => 'data Contact Sales not found!', 'code' => 400], 400);
            die();
        }


        if ($result) {
            return response()->json(['status' => 'success', 'message' => 'data Contact Sales successfully updated!', 'code' => 200], 200);
        } else {
            return response()->json(['status' => 'failed', 'message' => 'data Contact Sales failed updated!', 'code' => 400], 400);
        }
    }

    public function delete($id)
    {
        $data = ContactSales::find($id);

        if ($data) {
            $delete = $data->delete();
        } else {
            return response()->json(['status' => 'failed', 'message' => 'data Contact Sales not found!', 'code' => 400], 400);
            die();
        }


        if ($delete) {
            return response()->json(['status' => 'success', 'message' => 'data Contact Sales successfully deleted!', 'code' => 200], 200);
        } else {
            return response()->json(['status' => 'failed', 'message' => 'data Contact Sales failed deleted!', 'code' => 400], 400);
        }
    }

    public function search(Request $request)
    {
        $payload = $request->all();
        $dataBuilder = $this->setUpPayload($payload, 'm_contact_sales');
        $builder = $dataBuilder['builder'];
        $countBuilder = $dataBuilder['count'];
        $dataGet = $builder->get();
        $totalRecord = $countBuilder->count();
        return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
    }

    public function list()
    {
        $data = ContactSales::all();
        $countData = $data->count();

        if ($data) {
            return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
        } else {
            return response()->json(['status' => 'failed', 'message' => 'failed get list Contact Sales, please try again later!', 'code' => 400], 400);
        }
    }
}