import { Request, Response } from 'express';

export class ProspectiveLoginController {

    static async sendOtp(req: Request, res: Response) {
        try {
            const { phone } = req.body;

            if (!phone) {
                return res.status(400).json({ message: 'Phone number is required' });
            }

            // Mock logic: In a real app, we would generate a random OTP and send it via SMS
            // For now, we just return success
            console.log(`[Mock] OTP request for ${phone}`);

            return res.status(200).json({
                message: 'OTP sent successfully',
                data: {
                    phone,
                    // In development surfacing the OTP for easier testing
                    mockOtp: '123456'
                }
            });

        } catch (error) {
            console.error('Send OTP error:', error);
            return res.status(500).json({ message: 'Internal server error' });
        }
    }

    static async verifyOtp(req: Request, res: Response) {
        try {
            const { phone, otp } = req.body;

            if (!phone || !otp) {
                return res.status(400).json({ message: 'Phone and OTP are required' });
            }

            // Mock logic: Verify OTP
            if (otp === '123456') {
                // Mock success response with a fake token
                return res.status(200).json({
                    message: 'OTP verified successfully',
                    data: {
                        token: 'mock-prospective-token-' + Date.now(),
                        user: {
                            id: 'prospective-user-id',
                            phone: phone,
                            role: 'Prospective Dealer'
                        }
                    }
                });
            } else {
                return res.status(400).json({ message: 'Invalid OTP' });
            }

        } catch (error) {
            console.error('Verify OTP error:', error);
            return res.status(500).json({ message: 'Internal server error' });
        }
    }
}
