#!perl
use strict;
use warnings;

use Test::More;
use DBIx::Fast;

# Live MySQL (Oracle) smoke + upsert row-alias coverage. Gated on:
#   DBIX_FAST_TEST_MYSQL='mysql://root:test123@127.0.0.1:3308/dbix_test'
# Uses DBD::mysql (dbi:mysql), the driver DBIx::Fast selects for mysql://
# DSNs - distinct from the DBD::MariaDB paths covered elsewhere.

plan skip_all => "DBIX_FAST_TEST_MYSQL not set"
  unless $ENV{DBIX_FAST_TEST_MYSQL};

# No version floor: 4.050 (the last release that builds against the MariaDB
# client library shipped by Debian) is what dev environments realistically
# have; the mysql-specific code paths only need mysql_serverversion and
# mysql_insertid, present since long before it.
eval { require DBD::mysql; 1 }
  or plan skip_all => "DBD::mysql required";

my ($dsn) = $ENV{DBIX_FAST_TEST_MYSQL} =~ /^(.*)$/;   # untaint for -T

my $db = eval { DBIx::Fast->new( dsn => $dsn ) };
plan skip_all => "Cannot connect to MySQL ($dsn): $@" if $@ or !$db;

isa_ok( $db, 'DBIx::Fast' );
is( $db->dbd, 'mysql', 'mysql:// DSN resolves to the mysql driver' );
ok( $db->db->dbh->ping, 'live connection established' );
is( $db->val('SELECT 1'), 1, 'roundtrip query works' );

my $server = $db->db->dbh->{mysql_serverversion};
ok( $server, "mysql_serverversion available ($server)" );

# CRUD + last_id via mysql_insertid
$db->exec('DROP TABLE IF EXISTS mysql_probe');
$db->exec(q{
    CREATE TABLE mysql_probe (
        id   INT AUTO_INCREMENT PRIMARY KEY,
        k    VARCHAR(32) NOT NULL UNIQUE,
        v    INT
    )
});

my $id = $db->insert('mysql_probe', { k => 'a', v => 1 });
ok( $id, "insert returns last_id via mysql_insertid ($id)" );

# upsert: on MySQL >= 8.0.19 this emits the row-alias form
# (INSERT ... AS new ... UPDATE v = new.v); older servers fall back to
# VALUES(). Either way the row must be updated in place.
$db->upsert('mysql_probe', { k => 'a', v => 42 }, ['k']);
is( $db->val('SELECT v FROM mysql_probe WHERE k = ?', 'a'), 42,
    'upsert updated the conflicting row' );
is( $db->val('SELECT COUNT(*) FROM mysql_probe'), 1,
    'no duplicate row created' );

$db->upsert('mysql_probe', { k => 'b', v => 7 }, ['k']);
is( $db->val('SELECT COUNT(*) FROM mysql_probe'), 2,
    'upsert inserts a fresh row' );

# Native profiler: Profile::mysql (inherits the MariaDB implementation;
# MySQL 8.x keeps that diagnostics surface, deprecated but functional).
{
    my @warnings;
    local $SIG{__WARN__} = sub { push @warnings, @_ };
    my $p = $db->profiler;
    isa_ok( $p, 'DBIx::Fast::Profile::mysql' );
    is_deeply( \@warnings, [], 'no "using base profile" fallback warning' );

    my $plan = $p->explain_query('SELECT * FROM mysql_probe WHERE k = ?', 'a');
    ok( $plan && @$plan, 'explain_query returns a plan' );

    my $ts = $p->get_table_stats('mysql_probe');
    is( $ts->[0]{table_name}, 'mysql_probe', 'get_table_stats resolves the table' );

    my $conns = $p->get_current_connections;
    ok( $conns->{total_connections} >= 1, 'connection stats available' );
}

$db->exec('DROP TABLE IF EXISTS mysql_probe');

done_testing();
