#!/usr/bin/perl -w
# script to test the revision history functionality
# ARGS: table expected_history num_tests
# NOTE: bulk copied from cgi-bin/zymonic.pl

# TODO rewrite/replace with new dynamic fields if needed.

use strict;

# Library path
BEGIN
{
    use vars qw($location $script_name $script_location $utils);
    use Zymonic::Utils qw(clean debug death_handler);
    $main::SIG{__DIE__} = \&death_handler;
}

use Data::Dumper;

# Modules
use Zymonic::Config;
use Zymonic::Session;
use Zymonic::DB;
use Zymonic::Auth;
use XML::Simple;

my $protocol = ( ( defined( $ENV{HTTPS} ) and $ENV{HTTPS} eq 'on' ) ? 'https' : 'http' );

# Clean path
$ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/sbin:/usr/sbin";

# Create a session (CGI only)
$ENV{SERVER_NAME} = clean( $ENV{SERVER_NAME}, "-_" );
our $session = Zymonic::Session->new(
    ip_address => clean( $ENV{REMOTE_ADDR} ),
    protocol   => $protocol
);

my $system = $session->system_name( $ENV{SERVER_NAME}, "/etc/zymonic" );

print "Zymonic starting... system: $system\n";
my $config = Zymonic::Config->new(
    system_name => $system,
    config_dir  => "/etc/zymonic",
    ip_address  => clean( $ENV{REMOTE_ADDR} ),
    protocol    => $protocol
);

my $db = Zymonic::DB->new( config => $config );

$session->DB($db);

my $auth = Zymonic::Auth->new(
    config      => $config,
    session     => $session,
    DB          => $db,
    user        => '',
    credentials => '',
    logged_in   => '',
    ip_address  => $session->ip_address
);

# Initialise the session fully
$session->initialise_session(
    {
        config => $config,
        auth   => $auth,
        DB     => $db
    }
);

# get arguments
if ( @ARGV != 1 )
{
    die "Usage: num_tests\n";
}

my $num_tests = $ARGV[0];

my $layout_tbl = Zymonic::Table->new(
    table  => 'zz_layouts',
    ident  => 'zz_layouts',
    config => $config,
    auth   => $auth,
    db     => $db
);

# add a record
my $layout_name = '##TESTING##';
print "Adding layout ##TESTING##\n";

# clean up any existing testing records
$db->run_statement(
    {
        string => "DELETE FROM zz_layouts WHERE layoutname = ?",
        params => [$layout_name],
    }
);

# build record to add
my $layout_name_field;
foreach my $tbl_field ( @{ $layout_tbl->fields } )
{
    if ( $tbl_field->addname eq 'layoutname' )
    {
        $layout_name_field = $tbl_field;
        $tbl_field->{value} = $layout_name;
    }
    else
    {
        $tbl_field->{value} = '';
    }
}
$layout_tbl->before_add;
$layout_tbl->add_record;

# lookup record id
# table->records functionality not yet working, so use SQL to get it
my $layout_id = $db->run_query(
    {
        string => "SELECT * FROM zz_layouts WHERE layoutname = ?",
        params => [$layout_name],
    }
);
$layout_id = $layout_id->[0]->{layoutid};

$layout_tbl->after_add($layout_id);

# update it $num_test times
for ( my $i = 0 ; $i < $num_tests ; ++$i )
{
    $layout_name = join( '', map { ( 'a' .. 'z' )[ rand(26) ] } ( 1 .. 10 ) );
    print "Test No " . ( $i + 1 ) . ": updating name to $layout_name\n";

    $layout_tbl->before_update;
    $layout_name_field->{value} = $layout_name;
    $layout_tbl->update_record($layout_id);
    $layout_tbl->after_update($layout_id);
}

# display revision_history table
my $revs = $db->run_query(
    {
        string => "SELECT * FROM zz_layouts_rh WHERE layoutid = ?",
        params => [$layout_id],
    }
);

print "Revision History:\n" . Dumper($revs) . "\n";

# remove records from layouts revision history
$db->run_statement(
    {
        string => "DELETE FROM zz_layouts_rh WHERE layoutid = ?",
        params => [$layout_id],
    }
);

# remove record from layouts table
$layout_tbl->before_delete($layout_id);
$layout_tbl->delete_record($layout_id);

