<?php

declare(strict_types=1);

require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';
require_once __DIR__ . '/FakeMetadataProvider.php';

use FecaMailshots\Application\DslCompiler;
use FecaMailshots\Domain\DslParser;
use FecaMailshots\Tests\Unit\FakeMetadataProvider;

$metadata = new FakeMetadataProvider([
    'accounts' => ['id', 'name', 'account_type_id', 'sector_id', 'public_location_id'],
    'contacts' => ['id', 'account_id', 'contact_email_1'],
]);

$parser = new DslParser();
$compiler = new DslCompiler($metadata);

$ast = $parser->parse("accounts and contacts where accounts.account_type_id = 1");
$compiled = $compiler->compile($ast);
$sql = (string) ($compiled['sql'] ?? '');

if (strpos($sql, 'picklist_') !== false) {
    fwrite(STDERR, "Did not expect implicit picklist joins in physical-schema projection\n");
    exit(1);
}
if (strpos($sql, 's_accounts.`account_type_id` AS `accounts.account_type_id`') === false) {
    fwrite(STDERR, "Expected physical accounts.account_type_id projection\n");
    exit(1);
}
if (strpos($sql, 's_accounts.`account_type_id` = ?') === false) {
    fwrite(STDERR, "Expected predicate accounts.account_type_id to compile against physical column\n");
    exit(1);
}
echo "DSL accounts physical-schema regression test passed\n";
