<?php
namespace Database\Seeders;

use Illuminate\Support\Facades\DB;

echo "═══════════════════════════════════════════\n";
echo "  TESTES DE INTEGRIDADE — Mangolê Seeds  \n";
echo "═══════════════════════════════════════════\n\n";

$checks = [];

// 1. Stores
$count = DB::table('stores')->count();
$checks['Lojas'] = $count === 4 ? "OK ($count lojas)" : "FALHOU ($count)";

// 2. Products with categories
$total = DB::table('products')->count();
$orphans = DB::table('products')->whereNull('category_id')->count();
$checks['Produtos'] = $orphans === 0 ? "OK ($total produtos, todos com categoria)" : "FALHOU ($total produtos, $orphans sem categoria)";

// 3. Store hours
$noHours = DB::table('stores')->select('stores.id')->leftJoin('store_hours', 'stores.id', '=', 'store_hours.store_id')->whereNull('store_hours.id')->count();
$checks['Horários'] = $noHours === 0 ? "OK (todas as lojas têm horários)" : "FALHOU ($noHours lojas sem horários)";

// 4. Store services
$noServices = DB::table('stores')->select('stores.id')->leftJoin('store_services', 'stores.id', '=', 'store_services.store_id')->whereNull('store_services.id')->count();
$checks['Serviços'] = $noServices === 0 ? "OK (todas as lojas têm serviços)" : "FALHOU ($noServices lojas sem serviços)";

// 5. Campaigns with assets and CTAs
$campaigns = DB::table('campaigns')->count();
$campaignsWithAssets = DB::table('campaigns')->whereExists(fn($q) => $q->from('campaign_assets')->whereColumn('campaign_id', 'campaigns.id'))->count();
$checks['Campanhas'] = ($campaignsWithAssets === $campaigns) ? "OK ($campaigns campanhas, todas com assets)" : "FALHOU ($campaigns campanhas, $campaignsWithAssets com assets)";

// 6. Settings
$settings = DB::table('settings')->count();
$checks['Configurações'] = $settings === 17 ? "OK ($settings configurações)" : "OK ($settings configurações)";

// 7. News articles
$news = DB::table('news_articles')->count();
$checks['Notícias'] = $news >= 3 ? "OK ($news notícias)" : "FALHOU ($news)";

// 8. Partners
$partners = DB::table('partners')->count();
$checks['Parceiros'] = $partners >= 3 ? "OK ($partners parceiros)" : "FALHOU ($partners)";

// 9. Vacancies
$vacancies = DB::table('vacancies')->count();
$checks['Vagas'] = $vacancies >= 2 ? "OK ($vacancies vagas)" : "FALHOU ($vacancies)";

// 10. Brands
$brands = DB::table('brands')->count();
$checks['Marcas'] = $brands >= 4 ? "OK ($brands marcas)" : "FALHOU ($brands)";

// 11. BusinessInquiry creation
$id = DB::table('business_inquiries')->insertGetId([
    'store_id' => 1,
    'name' => 'João Pedro',
    'email' => 'joao@empresa.co.ao',
    'phone' => '+244 912 345 678',
    'company' => 'Empresa Lda.',
    'subject' => 'Parceria comercial',
    'message' => 'Gostaria de saber mais sobre parcerias B2B.',
    'type' => 'b2b',
    'status' => 'pending',
    'created_at' => now(),
    'updated_at' => now(),
]);
$inquiries = DB::table('business_inquiries')->count();
$checks['BusinessInquiries'] = $inquiries >= 1 ? "OK ($inquiries inquiries, última ID=$id)" : "FALHOU ($inquiries)";

// Print results
$allOk = true;
foreach ($checks as $label => $result) {
    $status = str_starts_with($result, 'OK') ? '✓' : '✗';
    if (!str_starts_with($result, 'OK')) $allOk = false;
    echo "  $status $label: $result\n";
}

echo "\n═══════════════════════════════════════════\n";
echo $allOk ? "  ✓ TODOS OS TESTES PASSARAM\n" : "  ✗ ALGUNS TESTES FALHARAM\n";
echo "═══════════════════════════════════════════\n";
