<?php

namespace App\Modules\TextMagic\Controllers;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use App\Modules\TextMagic\Services\TextMagicService;

class TextMagicApiController extends BaseController
{
    protected $textMagicService;

    public function __construct(TextMagicService $textMagicService)
    {
        $this->textMagicService = $textMagicService;
    }

    /**
     * Send SMS via TextMagic
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function send(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'send_to' => 'required|string|max:20',
            'message' => 'required|string|max:1600',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'error' => 'Validation failed: ' . $validator->errors()->first()
            ], 422);
        }

        try {
            $result = $this->textMagicService->sendSMS(
                $request->input('send_to'),
                $request->input('message')
            );

            if ($result['success']) {
                return response()->json([
                    'success' => true,
                    'message_id' => $result['message_id'] ?? null,
                    'status' => $result['status'] ?? 'sent',
                    'to' => $result['to'] ?? $request->input('send_to')
                ]);
            } else {
                return response()->json([
                    'success' => false,
                    'error' => $result['error'] ?? 'Failed to send SMS'
                ], 400);
            }

        } catch (\Exception $e) {
            Log::error('[TEXTMAGIC API] Send SMS failed', [
                'error' => $e->getMessage(),
                'to' => $request->input('send_to')
            ]);

            return response()->json([
                'success' => false,
                'error' => 'Internal error: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Test TextMagic credentials
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function testCredentials(Request $request)
    {
        try {
            $result = $this->textMagicService->testCredentials();

            if ($result['success'] && $result['valid']) {
                return response()->json([
                    'success' => true,
                    'valid' => true,
                    'account_info' => $result['user_info'] ?? null
                ]);
            } else {
                return response()->json([
                    'success' => false,
                    'valid' => false,
                    'error' => $result['error'] ?? 'Invalid credentials'
                ], 400);
            }

        } catch (\Exception $e) {
            Log::error('[TEXTMAGIC API] Test credentials failed', [
                'error' => $e->getMessage()
            ]);

            return response()->json([
                'success' => false,
                'error' => $e->getMessage()
            ], 500);
        }
    }
}
