<?php

namespace App\Controllers;

use App\Models\Order;
use App\Services\OrderProcessor;
use App\Services\TelegramService;
use App\Utils\View;

class OrderController
{
    public function index()
    {
        $orderModel = new Order();
        $orders = [];

        if (isset($_GET['q']) && !empty($_GET['q'])) {
            $search = $_GET['q'];
            $conn = \App\Config\Database::getInstance()->getConnection();

            // Search by ID, Title, or Domain Content
            // Using DISTINCT to avoid duplicates if multiple domains match in same order
            $sql = "SELECT DISTINCT o.* FROM orders o 
                    LEFT JOIN order_items oi ON o.id = oi.order_id 
                    LEFT JOIN domains d ON oi.domain_id = d.id 
                    WHERE o.id LIKE :search 
                    OR o.title LIKE :search 
                    OR d.name LIKE :search 
                    ORDER BY o.id DESC";

            $stmt = $conn->prepare($sql);
            $stmt->execute(['search' => "%$search%"]);
            $orders = $stmt->fetchAll();
        } else {
            $orders = $orderModel->findAll();
            // Sort by ID desc
            usort($orders, function ($a, $b) {
                return $b['id'] - $a['id'];
            });
        }

        // Pagination Logic
        $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $perPage = 30;
        $total = count($orders);
        $totalPages = ceil($total / $perPage);
        $offset = ($page - 1) * $perPage;
        $paginatedOrders = array_slice($orders, $offset, $perPage);

        View::render('layouts.admin', [
            'title' => 'Ordens Judiciais',
            'content' => __DIR__ . '/../../resources/views/admin/orders/index.php',
            'orders' => $paginatedOrders,
            'pagination' => [
                'current' => $page,
                'total' => $totalPages,
                'next' => ($page < $totalPages) ? $page + 1 : null,
                'prev' => ($page > 1) ? $page - 1 : null
            ]
        ]);
    }

    public function create()
    {
        View::render('layouts.admin', [
            'title' => 'Nova Ordem',
            'content' => __DIR__ . '/../../resources/views/admin/orders/create.php'
        ]);
    }

    public function store()
    {
        if (!isset($_FILES['csv_file']) || $_FILES['csv_file']['error'] !== UPLOAD_ERR_OK) {
            $_SESSION['flash_error'] = "Erro no upload do arquivo CSV.";
            View::redirect('/admin/orders/create');
            return;
        }

        $title = $_POST['title'];
        $type = $_POST['type'];
        $content = \App\Utils\TextFormatter::normalizeLineBreaks($_POST['content']);
        $received_at = $_POST['received_at'];

        $conn = \App\Config\Database::getInstance()->getConnection();

        try {
            // Create Order
            $sql = "INSERT INTO orders (title, type, content, received_at) VALUES (:title, :type, :content, :received_at)";
            $stmt = $conn->prepare($sql);
            $stmt->execute([
                'title' => $title,
                'type' => $type,
                'content' => $content,
                'received_at' => $received_at
            ]);

            $orderId = $conn->lastInsertId();

            // Process CSV
            $processor = new OrderProcessor();
            $count = $processor->process($orderId, $type, $_FILES['csv_file']['tmp_name']);

            // Send Telegram Notification
            $telegramService = new TelegramService();
            $typeLabel = ($type === 'block') ? 'Bloqueio' : (($type === 'unblock') ? 'Desbloqueio' : $type);

            $telegramService->sendOrderNotification([
                'id' => $orderId,
                'title' => $title,
                'type' => $typeLabel
            ], $count);

            $_SESSION['flash_success'] = "Ordem criada com sucesso! $count domínios processados.";
            View::redirect('/admin/orders');

        } catch (\Exception $e) {
            $_SESSION['flash_error'] = "Erro ao processar ordem: " . $e->getMessage();
            View::redirect('/admin/orders/create');
        }
    }

    public function view($id)
    {
        $orderModel = new Order();
        $order = $orderModel->find($id);

        if (!$order) {
            View::redirect('/admin/orders');
        }

        // Get Items
        $conn = \App\Config\Database::getInstance()->getConnection();
        $stmt = $conn->prepare("SELECT d.name, oi.action FROM order_items oi JOIN domains d ON oi.domain_id = d.id WHERE oi.order_id = :id");
        $stmt->execute(['id' => $id]);
        $items = $stmt->fetchAll();

        View::render('layouts.admin', [
            'title' => 'Detalhes da Ordem #' . $id,
            'content' => __DIR__ . '/../../resources/views/admin/orders/view.php',
            'order' => $order,
            'items' => $items
        ]);
    }
}
