<?php

namespace App\Console\Commands;

use App\Models\Category;
use App\Models\NewsComment;
use App\Models\AgencyBotCategory;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use App\Models\Setting;
use Illuminate\Support\Facades\File;

class SearchComments extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:searchComments';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Haber Kontrolleri Buradan Yapılıyor';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }



    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $newsComments=NewsComment::where('status',0)->get();
        foreach($newsComments as $newsComment) {

 
                                          $curlheader = curl_init();
                                          curl_setopt_array($curlheader, array(
                                            CURLOPT_URL => 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=AIzaSyBL3HEeBODwjnEvbgwWC3EYkzSKmqArWPM',
                                            CURLOPT_RETURNTRANSFER => true,
                                            CURLOPT_ENCODING => '',
                                            CURLOPT_MAXREDIRS => 10,
                                            CURLOPT_TIMEOUT => 0,
                                            CURLOPT_FOLLOWLOCATION => true,
                                            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                                            CURLOPT_CUSTOMREQUEST => 'POST',
                                            CURLOPT_POSTFIELDS =>'{
                                                  "contents": [
                                                             {
                                                              "role": "user",
                                                              "parts":[{ "text": "Sana Bir Haber Yorumu Paylaşacağım Bu Haber Yorumu Eğer Argo ve Spam yorumu ise bana sadece \'spam\' diye dönüş yapmanı eğer normal bir yorum ise \'spam değil\' diye dönüş yapmanı istiyorum"
                                                            }]},
                                                            {
                                                              "role": "model",
                                                              "parts":[{ "text": "Haber yorumunu bana verin."
                                                            }]},{
                                                              "role": "user",
                                                              "parts":[{ "text": "'.$newsComment->comment.'"
                                                            }]},
                                                            
                                                      ],
                                                  "safetySettings": [
                                                      {
                                                          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                                                          "threshold": "BLOCK_ONLY_HIGH"
                                                      }
                                                  ],
                                                  "generationConfig": {
                                                      "stopSequences": [
                                                          "Title"
                                                      ],
                                                      "temperature": 1.0,
                                                      "maxOutputTokens": 12000,
                                                      "topP": 0.8,
                                                      "topK": 10
                                                  }
                                              }',
                                            CURLOPT_HTTPHEADER => array(
                                              'Content-Type: application/json'
                                            ),
                                          ));

                                            $curlResponse = curl_exec($curlheader);
                                            $curlresult=json_decode($curlResponse);
                                             if(isset($curlresult)){
                                                $TitleresultMessage=$curlresult->candidates[0]->content->parts[0]->text;
                                                if($TitleresultMessage=="Spam" || $TitleresultMessage=="spam"){
                                                    $newsComment->delete();
                                                }
                                             
                                             }
                                        }


                              

        return Command::SUCCESS;
    }
}
