<?php

namespace App\Http\Controllers\Api\v2;

use App\Components\Functions;
use App\Models_v2\Message;
use App\Models_v2\RequestConfirm;
use App\Models_v2\Transaction;
use App\Notifications\NewMessage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TransactionController extends BaseController {
	//

	/**
	 * @api {post} /update-status Cập nhật trạng thái transaction
	 * @apiName update-status
	 * @apiGroup transaction
	 * @apiDescription Cập nhật trạng thái transaction
	 * @apiParam {String} id
	 *  * @apiParam {String} status
	 * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
	 * @apiSuccess {String} message  Tin nhắn hệ thống.
	 * @apiSuccess {String} data
	 */
	public function updateStatusTransaction( Request $request ) {
		$status = $request->input( 'status' );
		$id     = $request->input( 'id' );

		if ( $status == RequestConfirm::CONFIRMED ) {
			return response( [
				'status'  => 0,
				'message' => 'Dữ liệu không hợp lệ'
			] );
		}

		$requestConfirm = RequestConfirm::find( $id );
		if ( ! $requestConfirm ) {
			return response( [
				'status'  => 0,
				'message' => 'Dữ liệu không hợp lệ'
			] );
		}

		$accepts = $requestConfirm->accepts;
		if ( $status == RequestConfirm::CANCEL ) {
			if ( count( $accepts ) == 2 ) {
				return response( [
					'status'  => 0,
					'message' => 'Không thể hủy giao dịch 2 bên đã xác nhận'
				] );
			}
		}
		$requestConfirm->status = $status;
		$requestConfirm->save();

		$message = Message::query()
		                  ->where( 'request_confirm_id', $id )
		                  ->first();
		if ( $message ) {
			$pusher      = \PusherService::getClient();
			$retVal      = Functions::getMessageArr( $message );
			$userTrigger = $message->conversation->users()
			                                     ->where( 'users.id', '<>', $this->user->id )
			                                     ->first();


			$pusher->trigger( 'chat-v2-' . $this->user->id, 'v2-new-message', $retVal );
			if ( $userTrigger ) {
				$pusher->trigger( 'chat-v2-' . $userTrigger->id, 'v2-new-message', $retVal );
				if ( Functions::checkUserOnlineNoConversationV2( $userTrigger->id ) ) {
					\Notification::send( $userTrigger, new NewMessage(
						$this->user,
						$message
					) );
				}
			}
		}

		return response( [
			'status' => 1
		] );
	}

	/**
	 * @api {post} /history Lịch sử giao dịch
	 * @apiName history
	 * @apiGroup transaction
	 * @apiDescription Lịch sử giao dịch
	 * @apiParam {String} limit
	 *  * @apiParam {String} offset
	 * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
	 * @apiSuccess {String} message  Tin nhắn hệ thống.
	 * @apiSuccess {String} data
	 */
	public function getHistory( Request $request ) {
		$limit  = $request->input( 'limit', 10 );
		$offset = $request->input( 'offset', 0 );
		$items  = Transaction::query()
		                     ->where( 'user_id', $this->user->id )
		                     ->where('status', Transaction::STATUS_PROCESSED)
		                     ->limit( $limit )
		                     ->offset( $offset )
		                     ->orderBy( 'id', 'desc' )
		                     ->get();

		return response( [
			'status' => 1,
			'data'   => $items
		] );
	}
}
