#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

use CGI::Carp qw(fatalsToBrowser);
# use Date::Calc qw(:all);
# use DBI;

# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/transfer_changes_to_database.pl?1

#-------------------------------------------------------------------------------

my  @ARGV     = split(/\&/, $ENV{'QUERY_STRING'});
our $debug    = $ARGV[0] ;
# our $username = 'rory' ;

if ($debug) { print "Content-type: text/html\n\n"; }

$debug = 0 ;

#------------------------------------------------------------------------------------------

&today ;

&db_open_upd ;
# &db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
		&transfer_data("changes","event_quotes_changes") ; ;
		&transfer_data("changes_demos","demo_changes") ; ;
		&transfer_data("changes_analytics","analytics_changes") ; ;
		&transfer_data("changes_quotes","quotes_changes") ; ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

  
exit;

#------------------------------------------------------------------------------------------

sub transfer_data {
	
	my ($changes_script,$changes_table) = @_ ;  
	
	my %seen_changes = () ; my $saved_line_max_id = 0 ; my $most_recent_line ; my $cnt_line = 1 ; 
	
	&db_min_ro($changes_table,'*','','','') ;  
	
	foreach (sort {$b <=> $a} keys %{$db{$changes_table}}) {
		$seen_changes{$db{$changes_table}{$_}{changes}} = 1 ;
		$saved_line_max_id = $_ if $saved_line_max_id < $_ ;
		
		if  ($cnt_line == 1) {
			$cnt_line++ ;
			my @abc = split(/\|/,$db{$changes_table}{$_}{changes}) ;
			$most_recent_saved_line = $abc[0] ; 
		}
		
	}
 
    my $logfile = "/home/libs/data/logs/events/$changes_script.dat";

	open(my $fh, "<", $logfile) or die "Can't open $logfile: $!";
	
	my @lines = <$fh> ;  # Slurp all lines into an array
 
	# 202520250528164733|2025-05-28 16:47:33|event_quote_id='10427'|user_id=84|changed_from|last_update='2025-05-28 16:45:34',additional_notes='COURT 2 AND 3  Nkululeko 305 Aletia 31 5 COURT 2  Emihle 305 Nkululeko 31 5 COURT 3'
	# 202520250528164733|2025-05-28 16:47:33|event_quote_id='10427'|user_id=84|changed_from|last_update='2025-05-28 16:45:34',additional_notes='COURT 2 AND 3  Nkululeko 305 Aletia 315 COURT 2  Emihle 305 Nkululeko 315 COURT 3' 
 
	my $line_count = scalar @lines ;
	# my $line_count = $saved_line_max_id ;
	$line_count += $saved_line_max_id ;

	my %lines_ = () ; 
	

	
	foreach my $line (@lines) {
		chomp $line ;
		
		my @abc = split(/\|/,$line) ;
		$seen_changes{$line}  = 1 if $most_recent_saved_line && $abc[0] <= $most_recent_saved_line ;
		
		$line_count-- if $seen_changes{$line}   ;
		$lines_{$line} = 1 ;
	}
	
	foreach my $line (@lines) {
		chomp $line ;
		next if $seen_changes{$line} ;
		print "INSERT INTO $changes_table (id,changes) VALUES ($line_count,\"$line\") ; \n";
		$line_count-- ;
	}

	close($fh);
	
} #-------------------------------------------------------------------------------

use db ;
use today ;
use common ;
use common_min ;
