<?php

namespace OtomaticAi\Controllers\SingleMode;

use DOMDocument;
use DOMXPath;
use Exception;
use OtomaticAi\Controllers\Controller;
use OtomaticAi\Utils\RobotsTxt;
use OtomaticAi\Vendors\GuzzleHttp\Client;

class UrlSourceController extends Controller
{
    public function search()
    {
        $this->validate([
            "url" => ["required", "url"],
        ]);

        try {
            // create the client
            $client = new Client(
                [
                    "headers" => [
                        "User-Agent" => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
                    ]
                ]
            );

            if ($this->isDisabledByRobotsTxt($this->input("url"))) {
                $this->response(["message" => "The URL is disabled by the robots.txt"], 403);
            }

            $response = $client->get($this->input("url"));
            $response = $response->getBody()->getContents();

            $dom = new DOMDocument();
            @$dom->loadHTML($response);
            $xpath = new DOMXPath($dom);

            $title = null;

            $nodes = $xpath->query('//h1');
            if ($nodes->count() > 0) {
                $title = $nodes[0]->textContent;
            } else {
                $nodes = $xpath->query('//title');
                if ($nodes->count() > 0) {
                    $title = $nodes[0]->textContent;
                }
            }

            if (!empty($title)) {
                $this->response([
                    "title" => $title,
                    "url" => $this->input("url"),
                ]);
            } else {
                $this->response(["message" => "The title could not be found"], 403);
            }
        } catch (Exception $e) {
            $this->response(["message" => "An error occurred", "error" => $e->getMessage()], 503);
        }
    }

    /**
     * Determine if the scrap is disabled by the robots.txt
     *
     * @param string $url
     * @return boolean
     */
    private function isDisabledByRobotsTxt(string $url)
    {
        $robots = RobotsTxt::fromUrl($url);
        return !empty($robots->getRules("otomaticAI"));
    }
}
