<?php

namespace App\Console\Commands;

use App\Components\Functions;
use App\Models\Contract;
use App\Models\Hostel;
use App\Models\MoneyDetail;
use App\Models\MoneyInfo;
use App\Models\RenterRoom;
use App\Models\Room;
use Carbon\Carbon;
use Illuminate\Console\Command;

class CreateBillMonth extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'create:bill';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Command description';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle() {
		//
		//  $rooms = Room::where('id', 5347)->get();

		//$hostels = Hostel::where( 'owner_id', 6748 )->pluck( 'id' )->toArray();
		   $rooms = Room::all();

		//$rooms = Room::whereIn( 'hostel_id', $hostels )->get();
		//dd($rooms->pluck('id')->toArray());

		//$rooms = Room::where('id', 5350)->get();
		if ( Carbon::now()->day == 1 ) {

			foreach ( $rooms as $room ) {
				$hostelId = null;
				$roomId   = $room->id;
				if ( ! $room->hostel ) {
					continue;
				}


				if ( $room->hostel ) {
					$hostelId = $room->hostel->id;
				}

				$renters = RenterRoom::where( 'room_id', $room->id )->get();

				foreach ( $renters as $renter ) {

					if ( ! $renter->contract ) {
						continue;
					}

					if ( $renter->contract->status == Contract::LIQUIDATED ) {
						continue;
					}

					$contract = $renter->contract;
					$period   = Functions::getPeriodNumber( $contract->period );
//
					$startDate = Carbon::now()->startOfDay()->startOfMonth()->format( 'Y-m-d' );
					$endDate   = Carbon::now()->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->format( 'Y-m-d' );

					$range = $period;
					$name  = 'Thu tiền phòng từ ' . Carbon::now()->startOfDay()->startOfMonth()->format( 'd/m/Y' ) . ' đến ' .
					         Carbon::now()->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->format( 'd/m/Y' );

					if ( ! empty( $contract->start_date ) ) {
						if ( Carbon::now()->startOfDay()->startOfMonth()->lessThan( $contract->start_date ) ) {
							$range     = Functions::calculatePeriod2( $contract->start_date, Carbon::now()->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth() );
							$name      = 'Thu tiền phòng từ ' . optional($contract->start_date)->format( 'Y-m-d' ) . ' đến ' .
							             Carbon::now()->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->format( 'Y-m-d' );
							$startDate = optional($contract->start_date)->format( 'Y-m-d' );
						}
					}

					if ( ! empty( $contract->end_date ) ) {
						if ( Carbon::now()->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->greaterThan( $contract->end_date ) ) {
							$range   = Functions::calculatePeriod2( Carbon::now()->startOfDay()->startOfMonth(), $contract->end_date );
							$name    = 'Thu tiền phòng từ ' . Carbon::now()->startOfDay()->startOfMonth()->format( 'd/m/Y' ) . ' đến ' .
							           $contract->end_date->format( 'd/m/Y' );
							$endDate = $contract->end_date->format( 'Y-m-d' );
						}
					}

					$check = Functions::checkExistDate( $room->hostel, $startDate, $endDate, $contract, $room->id, $hostelId );
					// dump($name);
					if ( $check == 0 ) {
						$money     = intval( round( $contract->room_price * $range ) );
						$paidMoney = 0;
						$moneyInfo = MoneyInfo::create( [
							'hostel_id'       => $hostelId,
							'room_id'         => $roomId,
							'date_action'     => $startDate,
							'contract_id'     => $contract->id,
							'user_id'         => $renter->user_id,
							'type'            => MoneyInfo::VOUCHER_ROOM_PRICE,
							'amount'          => $money,
							'pay'             => $paidMoney,
							'remain'          => $money - $paidMoney,
							'money_info_name' => $name
						] );

						$moneyInfo->update( [
							'name' => 'HD' . $moneyInfo->id . Carbon::now()->format( 'dmY' )
						] );

						MoneyDetail::create( [
							'hostel_id'     => $hostelId,
							'room_id'       => $roomId,
							'name'          => $name,
							'value'         => $contract->room_price,
							'money_info_id' => $moneyInfo->id,
							'amount'        => $money,
							'note'          => $name,
							'start_date'    => isset( $startDate ) ? $startDate : null,
							'end_date'      => isset( $endDate ) ? $endDate : null,
							'qty'           => $period,
							'renter_id'     => $contract->renter_id
						] );
					}

					$this->line( 'Done with ' . $contract->id );


//					for ( $i = 1; $i <= 1; $i ++ ) {
//						$startDate = Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->format( 'Y-m-d' );
//						$endDate   = Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->format( 'Y-m-d' );
//
//						$range = $period;
//						$name  = 'Thu tiền phòng từ ' . Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->format( 'd/m/Y' ) . ' đến ' .
//						         Carbon::now()->startOfDay()->subMonth( $i )->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->format( 'd/m/Y' );
//
//						if ( ! empty( $contract->start_date ) ) {
//							if ( Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->lessThan( $contract->start_date ) ) {
//								$range     = Functions::calculatePeriod2( $contract->start_date, Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth() );
//								$name      = 'Thu tiền phòng từ ' . optional($contract->start_date)->format( 'Y-m-d' ) . ' đến ' .
//								             Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->format( 'Y-m-d' );
//								$startDate = optional($contract->start_date)->format( 'Y-m-d' );
//							}
//						}
//
//						if ( ! empty( $contract->end_date ) ) {
//							if ( Carbon::now()->startOfDay()->subMonth( $i )->startOfMonth()->addMonth( $period - 1 )->endOfMonth()->greaterThan( $contract->end_date ) ) {
//								$range   = Functions::calculatePeriod2( Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth(), $contract->end_date );
//								$name    = 'Thu tiền phòng từ ' . Carbon::now()->subMonth( $i )->startOfDay()->startOfMonth()->format( 'd/m/Y' ) . ' đến ' .
//								           $contract->end_date->format( 'd/m/Y' );
//								$endDate = $contract->end_date->format( 'Y-m-d' );
//							}
//						}
//
//						$check = Functions::checkExistDate( $room->hostel, $startDate, $endDate, $contract, $room->id, $hostelId );
//						// dump($name);
//						if ( $check == 0 ) {
//							$money     = intval( round( $contract->room_price * $range ) );
//							$paidMoney = 0;
//							$moneyInfo = MoneyInfo::create( [
//								'hostel_id'       => $hostelId,
//								'room_id'         => $roomId,
//								'date_action'     => $startDate,
//								'contract_id'     => $contract->id,
//								'user_id'         => $renter->user_id,
//								'type'            => MoneyInfo::VOUCHER_ROOM_PRICE,
//								'amount'          => $money,
//								'pay'             => $paidMoney,
//								'remain'          => $money - $paidMoney,
//								'money_info_name' => $name
//							] );
//
//							$moneyInfo->update( [
//								'name' => 'HD' . $moneyInfo->id . Carbon::now()->format( 'dmY' )
//							] );
//
//							MoneyDetail::create( [
//								'hostel_id'     => $hostelId,
//								'room_id'       => $roomId,
//								'name'          => $name,
//								'value'         => $contract->room_price,
//								'money_info_id' => $moneyInfo->id,
//								'amount'        => $money,
//								'note'          => $name,
//								'start_date'    => isset( $startDate ) ? $startDate : null,
//								'end_date'      => isset( $endDate ) ? $endDate : null,
//								'qty'           => $period,
//								'renter_id'     => $contract->renter_id
//							] );
//						}
//
//						$this->line( 'Done with ' . $contract->id );
////                    }
//					}
				}
			}
		}

	}
}
