#!/usr/bin/perl

# requires presence of config/.local/auto_reportable.yml file, or skips tests

use Test::WWW::Mechanize::CGIApp;

use Data::Dumper;
use YAML::Tiny;

use strict;
use warnings;

#use Test::More tests => 16; # can't - don't know if config file exists yet
use Test::More 'no_plan';

=begin: tests:
=cut

BEGIN {
    require 't/test-lib.pl';
}

my $yaml;
eval {
    my $src = './config/.local/auto_reportable.yml';
	$yaml = YAML::Tiny->read($src);
};

SKIP: {
    skip('these tests require config/.local/auto_reportable.yml file',1) unless $yaml;
    do_all_tests();
}

sub do_all_tests {

my $mech = get_mech();

do_login();

my $dbh;
eval {
    $dbh = get_dbh() or die 'no database handle recieved from get_dbh';
};

# register data for auto_authorisation:
my $dbix = get_dbix();

my $result_summary_option = 'sample shows evidence of Val617Phe JAK2 mutation';

# 2 new screening terms:
for ('CMPD pres & follow-up','CML imatinib PB follow-up') {
	$dbix->insert('screens', {
		description => $_, sample_type => 'fresh', active => 'yes',
	});
}

$dbix->insert('diagnoses', {
    name => 'Consistent with chronic myeloproliferative disorder',
    diagnostic_category_id => 1, active => 'yes',
});
$dbix->insert('diagnoses', {
    name => 'Laboratory tests only',
    diagnostic_category_id => 3, active => 'yes',
});


$dbix->insert('lab_tests', {
    field_label => 'JAK2', lab_section_id => 3, test_type => 'test',
    has_results => 'no', is_active => 'yes', test_name => 'jak2',
});
$dbix->insert('lab_tests', {
    field_label => 'CML', lab_section_id => 3, test_type => 'test',
    has_results => 'no', is_active => 'yes', test_name => 'cml',
});
$dbix->insert('result_summary_options', {
    description => $result_summary_option, lab_section_id => 3, is_active => 'yes',
});
$dbix->insert('screen_lab_test', {screen_id => 3, lab_test_id => 10}); # JAK2
$dbix->insert('screen_lab_test', {screen_id => 4, lab_test_id => 11}); # CML

# register new:
$mech->get_ok('/request/add_new/2');
{
    $mech->submit_form(
        fields => {
            request_number => 3,
            specimen => 'PB',
            referrer_code => 'C1234567',
        },
    );                                                        # print_and_exit();
    
    $mech->has_tag(
        h3 => 'New request successful',
    );
}

# screen request with auto-reportable term:
$mech->get_ok('/screen/=/3');                                 # print_and_exit();
{
    $mech->field(screen_id => 3);
    $mech->field(option_id => 1);
    $mech->submit();                                          # print_and_exit();
    
    $mech->has_tag_like(
        p => qr(Screened as CMPD pres & follow-up),
        'OK: expected screen term found',
    );    
}

# sign out test:
$mech->get_ok('/worklist/display/3?display_format=Data+Entry');
{
    $mech->field(request_lab_test_id => 1);
    $mech->field(status_option_id => 2);
    $mech->submit();                                          # print_and_exit();

    $mech->has_tag_like(
        p => qr(records updated successfully),
        'OK: update success',
    );                                                        # print_and_exit();
    
    for ( qw/JAK2 complete/ ) {
        $mech->has_tag(
            td => $_
        );
    }
}

# section result summary should trigger auto-report/authorisation:
$mech->get_ok('/result/=/3');                                 # print_and_exit();
{
    $mech->form_name('molecular_results');
    $mech->field( _results_summary => $result_summary_option);
    $mech->submit();                                          # print_and_exit();

    $mech->has_tag_like(
        p => qr(record updated successfully),
        'OK: update success',
    );                                                        # print_and_exit();
    
    # check 'reported by' & 'authorised by':
    $mech->has_tag_like(
        p => qr(Reported by),
        'OK: reporter information displayed'
    );
    $mech->has_tag_like(
        p => qr(Authorised by),
        'OK: authoriser information displayed'
    );														  # print_and_exit();
}

# register new:
$mech->get_ok('/request/add_new/2');
{
    $mech->submit_form(
        fields => {
            request_number => 4,
            specimen => 'PB',
            referrer_code => 'C1234567',
        },
    );                                                        # print_and_exit();
    
    $mech->has_tag(
        h3 => 'New request successful',
    );
}

# screen another request with auto-reportable term:
$mech->get_ok('/screen/=/4');                                 # print_and_exit();
{
    $mech->field(screen_id => 4);
    $mech->field(option_id => 1);
    $mech->submit();                                          # print_and_exit();
    
    $mech->has_tag_like(
        p => qr(CML imatinib PB follow-up),
        'OK: expected screen term found',
    );    
}

# section result summary should trigger auto-report/authorisation:
$mech->get_ok('/result/=/4');                                 # print_and_exit();
{
	my $result_summary = "BCR-ABL transcription number = 200\n"
	. "ABL transcription number = 20\n"
	. "BCR-ABL : ABL ratio = 2%";
	
    $mech->form_name('molecular_results');
    $mech->field(results_summary => $result_summary);
	$mech->tick('complete_all_tests', 1, 1);
	
    $mech->submit();                                         #  print_and_exit();

    $mech->has_tag_like(
        p => qr(record updated successfully),
        'OK: update success',
    );                                                        # print_and_exit();
    
	for ('Laboratory tests only', 'BCR-ABL transcription number', 'See RQ PCR result') {
		$mech->has_tag_like(
			td => qr($_),
			'OK: expected entry present',
		);                                                    # print_and_exit();
	}
	
    # check 'reported by' & 'authorised by':
    $mech->has_tag_like(
        p => qr(Reported by),
        'OK: reporter information displayed'
    );
}

$mech->get_ok('/history/=/4');                                # print_and_exit();
{
	for ('reported', 'new Molecular result summary') {
		$mech->has_tag_like(
			td => qr($_),
			'OK: expected history present'
		);
	}
}
do_logout();
}