<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Popup;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PopupController extends Controller {
	//
	/**
	 * @api {get} /list Lấy danh sách popup
	 * @apiName /list
	 * @apiGroup Popup
	 * @apiParam {String} type 1 là ios, 2 là android, 3 là web
	 *
	 * @apiDescription Api Lấy danh sách popup
	 * @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 getList( Request $request ) {
		$type      = $request->input( 'type' );

		$items = Popup::query()
		              ->where( 'start_time', '<=', Carbon::now()->startOfDay() )
		              ->where( 'end_time', '>=', Carbon::now()->endOfDay() )
		              ->orderBy( 'id', 'desc' );

		if ( ! empty( $type ) ) {
			$items = $items->where( 'type', $type );
		}

		$items = $items->where( 'status', Popup::ACTIVE )->get();

		return response( [
			'status'  => 1,
			'message' => 'Thành công',
			'data'    => $items
		] );
	}
}
