package Cyber::Cron;
use strict;
use warnings;

use Data::Dumper;
use String::Random;
use POSIX 'strftime';
use Encode;
use JSON;
use LWP::UserAgent;
use IO::Compress::Gzip qw(gzip);
use IO::Uncompress::Gunzip qw( gunzip );
use Cyber::Database;
use Date::Simple qw(date);
no warnings "uninitialized";
use HTTP::BrowserDetect;
use HTML::Entities;

#Constructor
sub new {
	my $class = shift;
	
	my $obj = {};
	
	my $obj_database = new Cyber::Database;
	$obj->{'obj_database'} = $obj_database;
	
	bless $obj, $class;
	
	return $obj;
}

#Authenticate User
sub get_set_lowest_rate {
	my $class = shift;
	
	my $today_date = ::get_today_day();

	#Check user name
	my $hotel_list = $class->{'obj_database'}->get({
		'table'  => 'hotels',
		'hash'   => 1,
		'select' => '*',
		'where'  => {
			'id' => 5,
			'hotel_status' => 'Y'
		}
	});
	
	unless ( scalar @$hotel_list ) {
		return;
	}
	
	if ( scalar @$hotel_list ) {
		foreach my $hotel ( @$hotel_list ) {
			my $lowest_rate_data = {};
			my $lowest_rack_rate = undef;

			my $hotel_id = $hotel->{'id'};

			##get inventory data
			my $whr->{'hotel_id'} = $hotel_id;
			$whr = [
				$whr,
				'AND',
				"date >= '$today_date'"
			];

			my $inventory_data = undef;
			eval {
				$inventory_data = $class->{'obj_database'}->get({
					'table'  => 'inventory_masters',
					'hash'   => 1,
					'select' => 'date, room_type_id, availability',
					'where'  => $whr
				});
			};

			my $final_inv_data = {};
			if ( scalar @$inventory_data ) {
				foreach my $inv ( @$inventory_data  ) {
					$final_inv_data->{$inv->{'room_type_id'}}->{$inv->{'date'}} = $inv->{'availability'};
				}
			}

			my $room_types_list = $class->{'obj_database'}->get_record_with_limit({
				'table'  => 'room_types',
				'hash'   => 1,
				'select' => '*',
				'where'  => {
					'hotel_id' => $hotel_id
				},
				'key' => 'id'
			});

			my $rate_plan_list = $class->{'obj_database'}->get({
				'table'  => 'rate_plans',
				'hash'   => 1,
				'select' => '*',
				'where'  => {
					'hotel_id' => $hotel_id
				}
			});

			if ( scalar @$rate_plan_list ) {

				foreach my $rate_plan ( @$rate_plan_list ) {
					my $room_type_id = $rate_plan->{'room_type_id'};
					my $rate_plan_id = $rate_plan->{'id'};
					my $rack_rate = $rate_plan->{'room_price'};

					if ( $room_types_list->{$room_type_id}->{'base_adults'} <= 1 ) {
						next;
					}

					##find lowest rack rate
					if ( defined $lowest_rack_rate ) {
						if ( $lowest_rack_rate > $rack_rate ) {
							$lowest_rack_rate = $rack_rate;
						}
					}
					else {
						$lowest_rack_rate = $rack_rate;
					}

					my $whr->{'hotel_id'} = $hotel_id;
					$whr->{'room_type_id'} = $room_type_id;
					$whr->{'rate_plan_id'} = $rate_plan_id;
					$whr->{'closed'} = 0;

					$whr = [
						$whr,
						'AND',
						"date >= '$today_date'",
						'AND',
						"base_amount > 0"
					];

					my $rate_restriction_data = undef;
					eval {
						$rate_restriction_data = $class->{'obj_database'}->get_record_with_limit({
							'table'  => 'rate_restriction_masters',
							'hash'   => 1,
							'select' => 'date, base_amount',
							'where'  => $whr,
							'key' => 'date'
						});
					};

					for (my $i = 0; $i < 365; $i++) {
						my $c_date = ::add_number_of_day( $today_date, $i );

						my $base_amount = undef;
						if ( exists $rate_restriction_data->{$c_date}->{'base_amount'} && $rate_restriction_data->{$c_date}->{'base_amount'} > 0 ) {
							$base_amount = $rate_restriction_data->{$c_date}->{'base_amount'};
						}
						# if ( !defined $base_amount || $base_amount =~ /null/i || $base_amount == 0 ) {
						# 	$base_amount = $lowest_rack_rate;
						# }

						if ( !defined $base_amount ) {
							next;
						}
						if ( $base_amount <= 0 ) {
							next;
						}
						
						if ( $final_inv_data->{$room_type_id}->{$c_date} > 0 ) {
							if ( exists $lowest_rate_data->{$c_date} ) {
								if ( $lowest_rate_data->{$c_date} > $base_amount ) {
									$lowest_rate_data->{$c_date} = $base_amount;
								}
							}
							else {
								$lowest_rate_data->{$c_date} = $base_amount;
							}
						}
						else {
							if ( exists $lowest_rate_data->{$c_date} ) {

							}
							else {
								$lowest_rate_data->{$c_date} = 1000000;
							}
						}


						# if ( exists $final_inv_data->{$room_type_id}->{$c_date} ) {
						# 	if ( $final_inv_data->{$room_type_id}->{$c_date} > 0 ) {
						# 		if ( scalar keys %$lowest_rate_data ) {
						# 			if ( exists $lowest_rate_data->{$c_date} ) {
						# 				if ( $lowest_rate_data->{$c_date} > $base_amount ) {
						# 					$lowest_rate_data->{$c_date} = $base_amount;
						# 				}
						# 			}
						# 			else {
						# 				$lowest_rate_data->{$c_date} = $base_amount;
						# 			}
						# 		}
						# 		else {
						# 			$lowest_rate_data->{$c_date} = $base_amount;
						# 		}
						# 	}
						# 	else {
						# 		$lowest_rate_data->{$c_date} = 1000000;
						# 	}
						# }
						# else {
						# 	if ( scalar keys %$lowest_rate_data ) {
						# 		if ( exists $lowest_rate_data->{$c_date} ) {
						# 			if ( $lowest_rate_data->{$c_date} > $base_amount ) {
						# 				$lowest_rate_data->{$c_date} = $base_amount;
						# 			}
						# 		}
						# 		else {
						# 			$lowest_rate_data->{$c_date} = $base_amount;
						# 		}
						# 	}
						# 	else {
						# 		$lowest_rate_data->{$c_date} = $base_amount;
						# 	}
						# }
					}
				}
			}

			for (my $i = 0; $i < 365; $i++) {
				my $c_date = ::add_number_of_day( $today_date, $i );

				if ( !exists $lowest_rate_data->{$c_date} ) {
					$lowest_rate_data->{$c_date} = $lowest_rack_rate;
				}
			}

			##get lowest rate data
			my $existing_lowest_rate_data = undef;
			eval {
				$existing_lowest_rate_data = $class->{'obj_database'}->get_record_with_limit({
					'table'  => 'lowest_rate_data',
					'hash'   => 1,
					'select' => 'date, lowest_rate',
					'where'  => {
						'hotel_id' => $hotel_id,
					},
					'key' => 'date'
				});
			};
			if ( scalar keys %$lowest_rate_data ) {
				foreach my $date ( keys %$lowest_rate_data ) {
					my $rate = $lowest_rate_data->{$date};
					if ( $rate == 1000000 ) {
						$rate = '-';
					}

					if ( exists $existing_lowest_rate_data->{$date} ) {
						eval {
							$class->{'obj_database'}->set(
								'table' => 'lowest_rate_data',
								'update' => {
									'lowest_rate'    => $rate,
									'modify_date'    => strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
								},
								'where' => {
									'hotel_id'       => $hotel_id,
									'date'           => $date,
								}
							);
						};
						if ( $@ ) {
							::log($@);
						}
					}
					else {
						eval {
							$class->{'obj_database'}->set(
								'table' => 'lowest_rate_data',
								'insert' => {
									'hotel_id'       => $hotel_id,
									'date'           => $date,
									'lowest_rate'    => $rate,
									'create_date'    => strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
									'modify_date'    => strftime("%Y-%m-%d %H:%M:%S", localtime(time())),
								},
							);
						};
						if ( $@ ) {
							::log($@);
						}
					}
				}
			}
		}
	}
}



1;
