use strict;
use warnings;

use Test::More;
use Test::Warn;
use DBIO::Test;

eval { require DateTime::Format::Pg; 1 }
  or plan skip_all => 'DateTime::Format::Pg not installed';

eval { require DateTime; 1 }
  or plan skip_all => 'DateTime not installed';

my ($dsn, $user, $pass) = @ENV{map { "DBIO_TEST_PG_${_}" } qw/DSN USER PASS/};

plan skip_all => 'Set $ENV{DBIO_TEST_PG_DSN}, _USER and _PASS to run this test'
  unless ($dsn && $user);

# Self-contained: build a minimal `event` table matching EventTZPg's
# declared columns, then connect the standard DBIO::Test::Schema (no_deploy
# avoids the full artists/cds/tracks install which would require a PG-aware
# test schema). Schema cleaned up in END via a dedicated dbh.
require DBI;
my $bootstrap = DBI->connect($dsn, $user, $pass, {
  AutoCommit => 1,
  RaiseError => 1,
  PrintError => 0,
});

$bootstrap->do('DROP TABLE IF EXISTS event CASCADE');
$bootstrap->do(q{
  CREATE TABLE event (
    id              integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    starts_at       date,
    created_on      timestamp with time zone,
    varchar_date    varchar(20),
    varchar_datetime varchar(20),
    skip_inflation  timestamp without time zone,
    ts_without_tz   timestamp without time zone
  )
});
$bootstrap->do(q{
  INSERT INTO event (starts_at, created_on, varchar_date, varchar_datetime)
  VALUES (current_date, '2009-01-15 11:00:00+00', '2009-02-01', '2009-02-01 12:00:00')
});
my $event_id = $bootstrap->selectrow_array('SELECT id FROM event LIMIT 1');
$bootstrap->disconnect;

END {
  return unless $dsn && $user;
  my $h = eval { DBI->connect($dsn, $user, $pass, { RaiseError => 0, PrintError => 0 }) };
  return unless $h;
  eval { $h->do('DROP TABLE IF EXISTS event CASCADE') };
  $h->disconnect;
}

DBIO::Test::Schema->load_classes({ 'DBIO::PostgreSQL::Test' => ['EventTZPg'] });

my $schema = DBIO::Test->init_schema(
  dsn       => $dsn,
  user      => $user,
  pass      => $pass,
  no_deploy => 1,
);

# this may generate warnings under certain CI flags, hence do it outside of
# the warnings_are below
my $dt = DateTime->new( year => 2000, time_zone => "America/Chicago" );

warnings_are {
  my $event = $schema->resultset("EventTZPg")->find($event_id);
  $event->update({created_on => '2009-01-15 17:00:00+00'});
  $event->discard_changes;
  isa_ok($event->created_on, "DateTime") or diag $event->created_on;
  is($event->created_on->time_zone->name, "America/Chicago", "Timezone changed");
  # Time zone difference -> -6hours
  is($event->created_on->iso8601, "2009-01-15T11:00:00", "Time with TZ correct");

# test 'timestamp without time zone'
  my $dt = DateTime->from_epoch(epoch => time);
  $dt->set_nanosecond(int 500_000_000);
  $event->update({ts_without_tz => $dt});
  $event->discard_changes;
  isa_ok($event->ts_without_tz, "DateTime") or diag $event->created_on;
  is($event->ts_without_tz, $dt, 'timestamp without time zone inflation');
  is($event->ts_without_tz->microsecond, $dt->microsecond,
    'timestamp without time zone microseconds survived');
} [], 'No warnings during DT manipulations';

done_testing;
