<?php

namespace Tests\Modules\BI;

use Tests\TestCase;

/**
 * Testes para o módulo BI
 */
class BITest extends TestCase {
    
    protected function setUp(): void {
        parent::setUp();
        
        // Criar tabelas necessárias para BI
        $this->pdo->exec('
            CREATE TABLE IF NOT EXISTS sales_orders (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                tenant_id VARCHAR(100) NOT NULL,
                total DECIMAL(18,2) NOT NULL,
                status VARCHAR(20) NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ');
        
        $this->pdo->exec('
            CREATE TABLE IF NOT EXISTS purchase_orders (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                tenant_id VARCHAR(100) NOT NULL,
                total DECIMAL(18,2) NOT NULL,
                status VARCHAR(20) NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ');
    }
    
    public function testCalculateTotalSales(): void {
        // Criar pedidos de venda
        $pedidos = [
            ['total' => 1000.00, 'status' => 'finalizado'],
            ['total' => 1500.00, 'status' => 'finalizado'],
            ['total' => 800.00, 'status' => 'finalizado'],
            ['total' => 500.00, 'status' => 'rascunho']
        ];
        
        foreach ($pedidos as $pedido) {
            $stmt = $this->pdo->prepare('
                INSERT INTO sales_orders (tenant_id, total, status)
                VALUES (?, ?, ?)
            ');
            $stmt->execute([$this->tenantId, $pedido['total'], $pedido['status']]);
        }
        
        // Calcular total de vendas
        $stmt = $this->pdo->prepare('
            SELECT SUM(total) as total_vendas
            FROM sales_orders
            WHERE tenant_id = ? AND status = "finalizado"
        ');
        $stmt->execute([$this->tenantId]);
        $result = $stmt->fetch();
        
        $this->assertEquals(3300.00, $result['total_vendas']);
    }
    
    public function testCalculateTotalPurchases(): void {
        // Criar pedidos de compra
        $pedidos = [
            ['total' => 500.00, 'status' => 'recebido'],
            ['total' => 300.00, 'status' => 'recebido'],
            ['total' => 200.00, 'status' => 'pendente']
        ];
        
        foreach ($pedidos as $pedido) {
            $stmt = $this->pdo->prepare('
                INSERT INTO purchase_orders (tenant_id, total, status)
                VALUES (?, ?, ?)
            ');
            $stmt->execute([$this->tenantId, $pedido['total'], $pedido['status']]);
        }
        
        // Calcular total de compras
        $stmt = $this->pdo->prepare('
            SELECT SUM(total) as total_compras
            FROM purchase_orders
            WHERE tenant_id = ? AND status = "recebido"
        ');
        $stmt->execute([$this->tenantId]);
        $result = $stmt->fetch();
        
        $this->assertEquals(800.00, $result['total_compras']);
    }
    
    public function testCalculateProfit(): void {
        // Criar vendas
        $stmt = $this->pdo->prepare('
            INSERT INTO sales_orders (tenant_id, total, status)
            VALUES (?, ?, ?)
        ');
        $stmt->execute([$this->tenantId, 1000.00, 'finalizado']);
        
        // Criar compras
        $stmt = $this->pdo->prepare('
            INSERT INTO purchase_orders (tenant_id, total, status)
            VALUES (?, ?, ?)
        ');
        $stmt->execute([$this->tenantId, 600.00, 'recebido']);
        
        // Calcular lucro
        $stmt = $this->pdo->prepare('
            SELECT 
                (SELECT COALESCE(SUM(total), 0) FROM sales_orders WHERE tenant_id = ? AND status = "finalizado") as vendas,
                (SELECT COALESCE(SUM(total), 0) FROM purchase_orders WHERE tenant_id = ? AND status = "recebido") as compras
        ');
        $stmt->execute([$this->tenantId, $this->tenantId]);
        $result = $stmt->fetch();
        
        $lucro = $result['vendas'] - $result['compras'];
        
        $this->assertEquals(400.00, $lucro);
    }
    
    public function testSalesByMonth(): void {
        // Criar vendas em diferentes meses
        $vendas = [
            ['total' => 1000.00, 'data' => date('Y-m-01')],
            ['total' => 1500.00, 'data' => date('Y-m-15')],
            ['total' => 800.00, 'data' => date('Y-m-20')]
        ];
        
        foreach ($vendas as $venda) {
            $stmt = $this->pdo->prepare('
                INSERT INTO sales_orders (tenant_id, total, status, created_at)
                VALUES (?, ?, ?, ?)
            ');
            $stmt->execute([$this->tenantId, $venda['total'], 'finalizado', $venda['data']]);
        }
        
        // Calcular vendas do mês
        $mes = date('Y-m');
        $stmt = $this->pdo->prepare('
            SELECT SUM(total) as total_mes
            FROM sales_orders
            WHERE tenant_id = ? 
            AND status = "finalizado"
            AND strftime("%Y-%m", created_at) = ?
        ');
        $stmt->execute([$this->tenantId, $mes]);
        $result = $stmt->fetch();
        
        $this->assertEquals(3300.00, $result['total_mes']);
    }
}

