<?php
/**
 * Outlet Helper class
 *
 * @package MultiPOS - Point of Sale for WooCommerce
 */

namespace DDWCMultiPOS\Helper\Outlet;

defined( 'ABSPATH' ) || exit();

if ( ! class_exists( 'DDWCPOS_Outlet_Helper' ) ) {
	/**
	 * Outlet Helper class.
	 *
	 * The free version is single-outlet by design: there is no outlets database table.
	 * The one outlet's configuration is stored as a single option array. Managing
	 * multiple outlets (and the outlets table) is a Pro feature.
	 */
	class DDWCPOS_Outlet_Helper {
		/**
		 * Option that stores the outlet configuration.
		 */
		const DDWCPOS_OUTLET_OPTION = '_ddwcpos_outlet';

		/**
		 * Fixed id used for the outlet.
		 */
		const DDWCPOS_OUTLET_ID = 1;

		/**
		 * Default outlet shape.
		 *
		 * @return array
		 */
		protected function ddwcpos_get_default_outlet() {
			return [
				'name'     => '',
				'mode'     => '',
				'address1' => '',
				'address2' => '',
				'city'     => '',
				'state'    => '',
				'country'  => '',
				'postcode' => '',
				'phone'    => '',
				'email'    => '',
				'payments' => '',
				'tables'   => '',
				'status'   => '',
				'created'  => '',
				'updated'  => '',
			];
		}

		/**
		 * Get the stored outlet, or null when it is not configured yet.
		 *
		 * @return array|null
		 */
		public function ddwcpos_get_outlet_option() {
			$outlet = get_option( self::DDWCPOS_OUTLET_OPTION, [] );

			if ( empty( $outlet ) || empty( $outlet['name'] ) ) {
				return null;
			}

			$outlet       = wp_parse_args( $outlet, $this->ddwcpos_get_default_outlet() );
			$outlet['id'] = self::DDWCPOS_OUTLET_ID;

			return $outlet;
		}

		/**
		 * Save the outlet configuration.
		 *
		 * @param array $data Outlet data.
		 * @return int
		 */
		public function ddwcpos_save_outlet( $data ) {
			$existing = get_option( self::DDWCPOS_OUTLET_OPTION, [] );
			$existing = is_array( $existing ) ? $existing : [];

			$data = wp_parse_args( $data, $existing );
			$data = wp_parse_args( $data, $this->ddwcpos_get_default_outlet() );

			$data['mode']    = in_array( $data['mode'], [ 'grocery', 'restaurant' ], true ) ? $data['mode'] : 'grocery';
			$data['tables']  = 'restaurant' === $data['mode'] ? $data['tables'] : maybe_serialize( [] );
			$data['id']      = self::DDWCPOS_OUTLET_ID;
			$data['created'] = ! empty( $existing['created'] ) ? $existing['created'] : current_time( 'Y-m-d H:i:s' );
			$data['updated'] = current_time( 'Y-m-d H:i:s' );

			update_option( self::DDWCPOS_OUTLET_OPTION, $data );

			return self::DDWCPOS_OUTLET_ID;
		}

		/**
		 * Get outlet details by id.
		 *
		 * @param int $id Outlet id.
		 * @return array|null
		 */
		public function ddwcpos_get_outlet_details_by_id( $id ) {
			$outlet = $this->ddwcpos_get_outlet_option();
			$data   = ( $outlet && self::DDWCPOS_OUTLET_ID === (int) $id ) ? $outlet : null;

			return apply_filters( 'ddwcpos_modify_outlets_data', $data, $id );
		}

		/**
		 * Get outlet modes.
		 *
		 * @return array
		 */
		public function ddwcpos_get_outlet_modes() {
			$outlet_modes = [
				'grocery'    => esc_html__( 'Grocery/Retail', 'devdiggers-multipos-for-woocommerce' ),
				'restaurant' => esc_html__( 'Restaurant/Cafe', 'devdiggers-multipos-for-woocommerce' ),
			];

			return apply_filters( 'ddwcpos_modify_outlet_modes', $outlet_modes );
		}
	}
}
