<?php
namespace App\Galileo\StructureParser\Structures\LCCTicketing;

trait LCCTrait
{
    /**
     * Retrieves a specific service segment from the A14 Galileo file line
     *
     * @param array $parsedStructure The parsed structure containing A14_LCC data
     * @param array $transactionIndexes Array containing indexes, including 'ks' (service index)
     * @return array The specific service segment from A14_LCC
     */
    protected function getA14Segment($parsedStructure, $transactionIndexes)
    {
        return $parsedStructure['A14_LCC']['Services'][$transactionIndexes['ks']];
    }

    protected function getA14FlightDetailsForSegment($parsedStructure, $transactionIndexes)
    {
        $A14Segment = $this->getA14Segment($parsedStructure, $transactionIndexes);
        $fareNumber = $A14Segment['PassengerFare'][$transactionIndexes['ks']]['fareNumber'];
        return array_filter($parsedStructure['A14_LCC']['FlightDetailsForSegment'], function($FD) use ($fareNumber) {
            return $FD['FlightDetailsForSegment']['fareNumber'] == $fareNumber;
        });
    }

    /**
     * Filters and retrieves passenger fare information from A14 service data
     * for a specific passenger identified by passengerIdentifier
     *
     * @param array $A14Service The A14 service segment
     * @param array $parsedStructure The complete parsed structure
     * @param array $transactionIndexes Array containing passenger identifier information
     * @return array Filtered array of passenger fares matching the passenger identifier
     */
    protected function getA14PassengerFare($A14Service, $parsedStructure, $transactionIndexes)
    {
        if ($A14Service['PassengerFare']) {
            return array_filter($A14Service['PassengerFare'], function($PF) use ($transactionIndexes) {
                return $transactionIndexes['passengerIdentifier'] == $PF['passengerIdentifier'];
            });
        }

        return [];
    }

    /**
     * Filters and retrieves other ancillary services from A14 service data
     * for a specific passenger identified by passengerIdentifier
     *
     * Uses in_array() as the passengerIdentifier in AncillariesLCC is an array
     *
     * @param array $A14Service The A14 service segment
     * @param array $parsedStructure The complete parsed structure
     * @param array $transactionIndexes Array containing passenger identifier information
     * @return array Filtered array of other ancillaries matching the passenger identifier
     */
    protected function getA14AncillariesLCC($A14Service, $parsedStructure, $transactionIndexes)
    {
        if ($parsedStructure['A14_LCC']['AncillariesLCC']) {
            return array_filter($parsedStructure['A14_LCC']['AncillariesLCC'], function($A) use ($transactionIndexes) {
                return $transactionIndexes['passengerIdentifier'] == $A['Ancillaries']['passengerIdentifier'];
            });
        }

        return [];
    }


    /**
     * Filters and retrieves Special Service Requests (SSR) from A14 service data
     * for a specific passenger identified by passengerIdentifier
     *
     * Uses in_array() as the passengerIdentifier in SSR is an array
     *
     * @param array $A14Service The A14 service segment
     * @param array $parsedStructure The complete parsed structure
     * @param array $transactionIndexes Array containing passenger identifier information
     * @return array Filtered array of SSRs matching the passenger identifier
     */
    protected function getA14SSR($A14Service, $parsedStructure, $transactionIndexes)
    {
        if ($parsedStructure['A14_LCC']['SSR']) {
            return array_filter($parsedStructure['A14_LCC']['SSR'], function($SSR) use ($transactionIndexes) {
                return in_array($transactionIndexes['passengerIdentifier'], $SSR['SSR']['passengerIdentifier'])
                        && $SSR['SSR']['type_']!='02';
            });
        }
        return [];
    }
}
