<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Hostel;
use App\Models\HostelPromotion;
use App\Models\Promotion;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HostelPromotionController extends BaseController {
	//
	/**
	 * @api {get} /list  Danh sách khuyến mại nhà trọ
	 * @apiName list
	 * @apiGroup HostelPromotion
	 * @apiDescription Api Danh sách khuyến mại nhà trọ
	 * @apiParam {String} hostel_id
	 * @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 index( Request $request ) {
		$ownerId = $this->user->id;
		if ( $this->user->type == User::STAFF ) {
			$ownerId = $this->user->staff_owner_id;
		}
		$hostelId   = $request->input( 'hostel_id' );
		$limit      = $request->input( 'limit', 10 );
		$offset     = $request->input( 'offset', 0 );
		$promotions = Promotion::query()
		                       ->when( ! empty( $hostelId ), function ( $q ) use ( $hostelId ) {
			                       $q->whereHas( 'hostels', function ( $q ) use ( $hostelId ) {
				                       $q->where( 'hostel_id', $hostelId );
			                       } );
		                       } )
		                       ->where( 'user_id', $this->user->id )
		                       ->limit( $limit )
		                       ->offset( $offset )
		                       ->get();

		return response( [
			'status'  => 1,
			'message' => 'Success',
			'data'    => $promotions
		] );

	}

	/**
	 * @api {post} /create  Tạo khuyến mại nhà trọ
	 * @apiName create
	 * @apiGroup HostelPromotion
	 * @apiDescription Api Tạo khuyến mại nhà trọ
	 * @apiParam {String} start_date Dạng d/m/Y
	 * @apiParam {String} end_date Dạng d/m/Y
	 * @apiParam {String} hostel_id
	 * @apiParam {String} title
	 * @apiParam {String} desc
	 *
	 * @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 create( Request $request ) {
		$data            = $request->except( [
			'start_date',
			'end_date',
			'hostel_id'
		] );
		$startDate       = $request->input( 'start_date' );
		$endDate         = $request->input( 'end_date' );
		$data['user_id'] = $this->user->id;

		if ( ! empty( $startDate ) ) {
			$data['start_date'] = Carbon::createFromFormat( 'd/m/Y', $startDate )->startOfDay()->toDateTimeString();
		}

		if ( ! empty( $endDate ) ) {
			$data['end_date'] = Carbon::createFromFormat( 'd/m/Y', $endDate )->endOfDay()->toDateTimeString();
		}
		$data['from'] = Promotion::FROM_OWNER;

		$promotion = Promotion::create( $data );
		$hostelIds = $request->input( 'hostel_id' );
		if ( is_array( $hostelIds ) ) {
			$promotion->hostels()->sync( $hostelIds );
		}

		return response( [
			'status'  => 1,
			'message' => 'Success'
		] );

	}

	/**
	 * @api {post} /update  Cập nhật khuyến mại nhà trọ
	 * @apiName update
	 * @apiGroup HostelPromotion
	 * @apiDescription Api Tạo khuyến mại nhà trọ
	 * @apiParam {String} start_date Dạng d/m/Y
	 * @apiParam {String} end_date Dạng d/m/Y
	 * @apiParam {String} hostel_id
	 * @apiParam {String} title
	 * @apiParam {String} desc
	 * @apiParam {String} id
	 *
	 * @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 update( Request $request ) {
		$data      = $request->except( [
			'start_date',
			'end_date',
			'hostel_id'
		] );
		$startDate = $request->input( 'start_date' );
		$endDate   = $request->input( 'end_date' );

		if ( ! empty( $startDate ) ) {
			$data['start_date'] = Carbon::createFromFormat( 'd/m/Y', $startDate )->startOfDay()->toDateTimeString();
		}

		if ( ! empty( $endDate ) ) {
			$data['end_date'] = Carbon::createFromFormat( 'd/m/Y', $endDate )->endOfDay()->toDateTimeString();
		}
		$data['from'] = Promotion::FROM_OWNER;

		$promotion = Promotion::find( $data['id'] );
		if ( $promotion ) {
			$promotion->update( $data );
		}

		$hostelIds = $request->input( 'hostel_id' );
		if ( is_array( $hostelIds ) ) {
			$promotion->hostels()->sync( $hostelIds );
		}

		return response( [
			'status'  => 1,
			'message' => 'Success'
		] );

	}

	/**
	 * @api {post} /delete  Xóa khuyến mại nhà trọ
	 * @apiName delete
	 * @apiGroup HostelPromotion
	 * @apiDescription Api Xóa khuyến mại nhà trọ
	 * @apiParam {String} id
	 *
	 * @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 destroy( Request $request ) {
		$id   = $request->input( 'id' );
		$item = Promotion::find( $id );
		if ( $item ) {
			$item->delete();
		}

		return response( [
			'status'  => 1,
			'message' => 'Success'
		] );

	}
}
