<?php
namespace Themes\kbgolf\pro\app\Http\Controllers\Auth;

use App\Services\CallApiService;
use App\Http\Controllers\Controller;
use Themes\kbgolf\pro\app\Services\SocialService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class SocialTalkController extends Controller
{
    protected $socialService;

    public function __construct(SocialService $socialService)
    {
        $this->socialService = $socialService;
    }

    public function sendMessage(Request $request)
    {
        Log::debug($request);
        $validatedData = $request->validate([
            'channel_id' => 'required|string',
            'template_object.text' => 'required|string',
            'link_url' => 'nullable|url',
        ]);
        // Log::debug('검증: ' . json_encode($validatedData));

        try {
            $response = $this->socialService->sendMessage(
                $validatedData['channel_id'],
                [
                    'message' => $validatedData['template_object']['text'],
                    'link_url' => $validatedData['link_url'] ?? null,
                ]
            );
            // Log::debug('결과: ' . $response);
            return response()->json(['success' => true, 'message' => '메시지가 성공적으로 전송되었습니다.', 'response' => json_decode($response)]);
        } catch (ValidationException $e) {
            return response()->json(['success' => false, 'message' => '유효성 검사 오류입니다.', 'errors' => $e->errors()]);
        } catch (Exception $e) {
            return response()->json(['success' => false, 'message' => '메시지 전송에 실패했습니다.', 'error' => $e->getMessage()]);
        }
    }
}
