/* fair_demand.pl */


fair_demand_daily( Level, FairStartDate, FairEndDate, TlStartDate, TlEndDate, Xs, Ys ) :-
  fair_demand_key_points( Level, FairStartDate, FairEndDate, KeyPoints),
  
  maplist( {TlStartDate} / [kp(Date,DI,_Reason), Offset-DI] >> date_interval(Date, TlStartDate, days(Offset))
         , KeyPoints
         , OffsetsAndDIs
         ),
  
  TlStartOffset = 0,
  date_interval(TlEndDate, TlStartDate, days(TlEndOffset)), 
  time_series_segments_to_points(TlStartOffset, TlEndOffset, OffsetsAndDIs, Xs, Ys).


fair_demand_key_points( Level, FairStartDate, FairEndDate, KeyPoints ) :-
  findall( kp( Date, DI, Reason )
         , fair_demand_key_point( Level, FairStartDate, FairEndDate, Date, DI, Reason )
         , KeyPoints
		 ).


fair_demand_key_point( [TPSym,ISSym,HASym], FairStartDate, FairEndDate, Date, DI, Reason ) :-
  fair_demand_symbolic( [TPSym,ISSym,HASym], DR, DI, Reason ),
  eval_daterel_fair( DR, FairStartDate, FairEndDate, Date ).
  
  
tp_symbol_range(high, 1.5, 2.0).
tp_symbol_range(medium, 1.1, 1.49).
tp_symbol_range(medium_low, 0.8, 1.09).
tp_symbol_range(low, 0.5, 0.79).


is_symbol_range(large, 1.5, 2.0).
is_symbol_range(medium_large, 1.2, 1.49).
is_symbol_range(medium, 0.9, 1.19).
is_symbol_range(small_medium, 0.7, 0.89).
is_symbol_range(small, 0.5, 0.69).

  
ha_symbol_range(very_high, 2.0, 2.0).
ha_symbol_range(high, 1.5, 1.99).
ha_symbol_range(medium_high, 1.2, 1.49).
ha_symbol_range(medium, 0.9, 1.19).
ha_symbol_range(low_medium, 0.7, 0.89).
ha_symbol_range(low, 0.5, 0.69).


fair_demand_symbolic( [tp-TPSym,is-ISSym,ha-HASym], DR, DI, Reason ) :- !,
  tp_symbol_range( TPSym, TPLow, TPHigh ),
  is_symbol_range( ISSym, ISLow, ISHigh ),
  ha_symbol_range( HASym, HALow, HAHigh ),   
  TPNum is (TPLow + TPHigh) / 2.0,
  ISNum is (ISLow + ISHigh) / 2.0,
  HANum is (HALow + HAHigh) / 2.0,
  fair_demand( [TPNum,ISNum,HANum], DR, DI, Reason ).
  

% Before the fair starts.
fair_demand( [_,_,_], 'start'-10, 0, "Just before early arrivals for setup and pre-fair meetings." ).
	
fair_demand( [TP,IS,HA], 'start'-7, DI, "Early arrivals for setup and pre-fair meetings." ) :-
    DI is 5 + TP*0.5 + IS*0.3 + HA*0.01.

fair_demand( [TP,IS,HA], 'start'-3, DI, "Attendees arriving for pre-fair workshops or networking." ) :-
    DI is 10 + TP*0.7 + IS*0.5 + HA*0.015.

fair_demand( [TP,IS,HA], 'start', DI, "Opening day of the fair, highest influx of attendees." ) :-
    DI is 20 + TP + IS*0.7 + HA*0.02.

% During the fair.
fair_demand( [TP,IS,HA], 'mid', DI, "Mid-fair, continued high demand for flights." ) :-
    DI is 15 + TP*0.9 + IS*0.6 + HA*0.018.

fair_demand( [TP,IS,HA], 'end'-2, DI, "As the fair concludes, last-minute attendees and ongoing activities." ) :-
    DI is 10 + TP*0.8 + IS*0.4 + HA*0.015.

% After the fair ends.
fair_demand( [_,_,_], 'end'+2, 0, "Arrivals end as the fair wraps up." ).
	
	
eval_daterel_fair( 'start', FairStartDate, _FairEndDate, FairStartDate ).

eval_daterel_fair( 'mid', FairStartDate, FairEndDate, FairMidDate ) :- 
  date_interval( FairEndDate, FairStartDate, days(Diff) ),
  HalfDiff is floor(Diff),
  date_add( FairStartDate, days(HalfDiff), FairMidDate ). 

eval_daterel_fair( 'end', _FairStartDate, FairEndDate, FairEndDate ). 

eval_daterel_fair( DR+Offset, FairStartDate, FairEndDate, Date ) :-
  eval_daterel_fair( DR, FairStartDate, FairEndDate, DRDate ),
  date_add( DRDate, days(Offset), Date ).
  
eval_daterel_fair( DR-Offset, FairStartDate, FairEndDate, Date ) :-
  eval_daterel_fair( DR, FairStartDate, FairEndDate, DRDate ),
  date_add( DRDate, days(-Offset), Date ).