<?php

namespace App\Console\Commands;

use App\Models\CollectSpend;
use App\Models\Contract;
use App\Models\Hostel;
use App\Models\MoneyDetail;
use App\Models\MoneyInfo;
use App\Models\Renter;
use App\Models\RenterBike;
use App\Models\RenterRoom;
use App\Models\Statistic;
use App\Models\Transaction;
use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        //
	    $contractId = 8678;
	    $item = Contract::find($contractId);
	    $contract = Contract::query()->withTrashed()->where('id', $contractId)->restore();
	    $collectSpends = CollectSpend::query()->withTrashed()->where( 'contract_id', $contractId )->get();

	    foreach ( $collectSpends as $collectSpend ) {
		    $transactionId = $collectSpend->transaction_id;
		    $transaction   = Transaction::withTrashed()->find( $transactionId );
		    if ( $transaction ) {
			    $transaction->restore();
		    }
	    }
	    CollectSpend::query()->withTrashed()->where( 'contract_id', $contractId )->restore();

	    $moneyInfos = MoneyInfo::query()->withTrashed()->where( 'contract_id', $contractId )->get();

	    foreach ( $moneyInfos as $moneyInfo ) {
		    MoneyDetail::query()->withTrashed()->where( 'money_info_id', $moneyInfo->id )->restore();
		    CollectSpend::query()->withTrashed()->where( 'money_info_id', $moneyInfo->id )->restore();
		    Statistic::query()->withTrashed()->where( 'money_info_id', $moneyInfo->id )->restore();
	    }

	    $room = $item->room;
	    $roomId = $room->id;

	    MoneyInfo::query()->withTrashed()->where( 'contract_id', $contractId )->restore();

	    RenterRoom::query()->withTrashed()->where( 'user_id', $item->renter_id )->where( 'room_id', $roomId )->restore();
	  //  RenterBike::query()->withTrashed()->where( 'renter_id', $item->renter_id )->where( 'room_id', $roomId )->restore();

    }
}
