<?php

namespace App\Http\Controllers;

use App\Models\OwnerMaster;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Carbon\Carbon;

class OwnerAuthController extends Controller
{

    // ==========================================
    // SEND OTP
    // ==========================================
    public function sendOtp(Request $request)
    {
        $request->validate([
            'login' => 'required'
        ]);

        // Find by mobile or email
        $owner = OwnerMaster::where('Mobile_1', $request->login)
                    ->orWhere('Email', $request->login)
                    ->first();

        if (!$owner) {
            return response()->json([
                'status' => false,
                'message' => 'Owner not found'
            ], 404);
        }

        // Generate OTP
        $otp = rand(100000, 999999);

        // Save OTP
        $owner->otp = $otp;
        $owner->otp_expiry = Carbon::now()->addMinutes(5);
        $owner->save();

        // =====================================
        // SEND EMAIL OTP
        // =====================================

        if (filter_var($request->login, FILTER_VALIDATE_EMAIL)) {

            Mail::raw("Your OTP is: $otp", function ($message) use ($owner) {

                $message->to($owner->Email)
                        ->subject('Login OTP');
            });
        }

        // =====================================
        // SEND MOBILE OTP
        // =====================================

        // Integrate SMS gateway here

        return response()->json([
            'status' => true,
            'message' => 'OTP sent successfully',
            'otp_for_testing' => $otp
        ]);
    }

    // ==========================================
    // VERIFY OTP
    // ==========================================

    public function verifyOtp(Request $request)
    {
        $request->validate([
            'login' => 'required',
            'otp'   => 'required'
        ]);

        $owner = OwnerMaster::where(function ($q) use ($request) {

                    $q->where('Mobile_1', $request->login)
                      ->orWhere('Email', $request->login);

                })
                ->where('otp', $request->otp)
                ->first();

        if (!$owner) {

            return response()->json([
                'status' => false,
                'message' => 'Invalid OTP'
            ], 401);
        }

        // Check expiry
        if (Carbon::now()->gt($owner->otp_expiry)) {

            return response()->json([
                'status' => false,
                'message' => 'OTP expired'
            ]);
        }

        // Clear OTP
        $owner->otp = null;
        $owner->otp_expiry = null;
        $owner->save();

        // Generate token
        $token = Str::random(60);

        return response()->json([
            'status' => true,
            'message' => 'Login successful',
            'token' => $token,
            'data' => $owner
        ]);
    }


}


