<?php

namespace App\Console\Commands;

use App\Models\Contract;
use App\Models\Deposit;
use App\Models\RoomReservation;
use Illuminate\Console\Command;

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

	/**
	 * 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() {
		//
		Deposit::query()->truncate();
		$reserves = RoomReservation::query()->get();
		$this->line( 'Start reserve' );
		$bar = $this->output->createProgressBar( $reserves->count() );
		$bar->start();
		foreach ( $reserves as $reserve ) {
			Deposit::create( [
				'reserve_id'  => $reserve->id,
				'amount'      => $reserve->deposit,
				'date_action' => $reserve->created_at,
				'room_id'     => $reserve->room_id,
				'hostel_id'   => $reserve->hostel_id,
			] );
			$bar->advance();
		}

		$bar->finish();


		$this->line( 'Start contract' );
		$contracts = Contract::query()->where( 'status', '<>', Contract::LIQUIDATED )
		                     ->where( 'deposit', '>', 0 )
		                     ->where( 'is_return_deposit', false )
		                     ->get();

		$bar = $this->output->createProgressBar( $contracts->count() );
		foreach ( $contracts as $contract ) {
			Deposit::create( [
				'contract_id' => $contract->id,
				'amount'      => $contract->deposit,
				'date_action' => $contract->created_at,
				'room_id'     => $contract->room_id,
				'hostel_id'   => $contract->hostel_id,
			] );

			$bar->advance();
		}

		$bar->finish();
	}
}
