<?php

namespace Modules\Contact\app\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;
use App\Models\Contact;
use Illuminate\Support\Facades\Mail;
use Modules\Contact\app\Mail\ContactMail;
use Modules\Admin\app\Models\Setting;

class ContactController extends Controller
{
    public function __construct()
    {
        if (env('APP_ENV') === 'prod' || env('APP_ENV') === 'production') {
            $this->middleware([
                'throttle:5,3' // 5 times in 3 minutes, apply only 1 device
            ])->only([
                'create'
            ]);
        }
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('contact::index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email|max:100',
            'name' => 'required|max:100',
            'company' => 'nullable|max:100',
            'phone' => 'nullable|max:100',
            'message' => 'required|max:500',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }
        //
        $existing = Contact::where('email', $request->input('email'))->first();
        $contactData = null;

        if ($existing) {
            $existing->name = $request->input('name');
            $existing->company = $request->input('company');
            $existing->phone = $request->input('phone');
            $existing->message = $request->input('message');
            $existing->save();
            $contactData = $existing;
        } else {
            $data = Contact::create([
                'email' => $request->input('email'),
                'name' => $request->input('name'),
                'company' => $request->input('company'),
                'phone' => $request->input('phone'),
                'message' => $request->input('message'),
            ]);
            $contactData = $data;
        }

        //Data Send mail
        $dataSendMail = [
            "email" => $contactData->email,
            "name" => $contactData->name,
            "phone" => $contactData->phone,
            "company" => $contactData->company,
            "message" => $contactData->message,
        ];

        $setting = Setting::first();

        Mail::to($setting->email)->send(new ContactMail($dataSendMail));
        return response()->json([
            'message' => 'Send mail successfully',
            'status' => 200,
            'data' => $dataSendMail
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('contact::show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        return view('contact::edit');
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        //
    }
}
