<?php

namespace App\Http\Controllers\Web\BecomeSeller;

use App\Http\Controllers\Controller;
use App\Http\Requests\Web\BecomeSellerPageStoreRequest;
use App\Models\EntrepreneursDocs;
use App\Models\User;
use App\Models\Verification;
use Illuminate\Auth\Events\Verified;
use Illuminate\Http\Request;

class BecomeSellerPageController extends Controller
{
    public function index()
    {
        return view('web.themes.shella.pages.becomeseller.index');
    }

    public function store(BecomeSellerPageStoreRequest $request)
    {
        $user = User::create([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
            'phone_number' => $request->phone_number,
        ]);

        $user->addresses()->forceCreate([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'company_name' => $request->shop_name,
            'street_address' => $request->street_address,
            'zipcode' => 744000,
            'city' => $request->city,
            'phone_number' => $request->phone_number,
            'is_default' => true,
            'type' => 'work',
        ]);

        EntrepreneursDocs::create([
            'corporation_type' => $request->corporation_type,
            'corporation_name' => $request->corporation_name,
            'patent_data' => str_replace('public/', '', $request->file('patent_data')?->store('public/entrepreneur/patent_data') ?? 'public/'),
            'user_id' => $user->id,
        ]);

        sendSMSVerification($request->phone_number);

        $verification = Verification::where(['username' => $request->email])->first();
        $verification ? $verification->update(['code' => 12345]) : Verification::create(['username' => $request->email, 'code' => 12345]);

        return to_route('web.become-seller.verification', ['phone' => $request->phone_number, 'email' => $request->email]);
    }

    public function verification()
    {
        return view('web.themes.shella.pages.becomeseller.verification');
    }

    public function verificationStore(Request $request)
    {
        $request->validate([
            'phone_number' => ['required', 'integer', 'between:61000000,65999999'],
            'phone_verification_code' => ['required', 'string'],
            'email' => ['required', 'string', 'email', 'exists:users,email'],
            'email_verification_code' => ['required', 'string'],
        ]);

        if (Verification::where('username', $request->phone_number)->where('code', $request->phone_verification_code)->exists()
                && Verification::where('username', $request->email)->where('code', $request->email_verification_code)->exists()
        ) {
            $user = User::where([
                'phone_number' => $request->phone_number,
                'email' => $request->email,
            ])->update([
                'phone_number_verified_at' => now(),
                'email_verified_at' => now(),
            ]);

            event(new Verified($user));

            session()->flash('sweet_alert_message', true);

            session()->flash('sweet_alert_message_title', 'Üstünlikli ýerine ýetirildi');
            session()->flash('sweet_alert_message_content', '');
            session()->flash('sweet_alert_message_status', 'success');

            return to_route('web.home');
        }

        return back()->withErrors([
            'Verification' => 'could not pass validation',
        ]);
    }
}
