<?php

namespace App\Http\Controllers;

use App\Jobs\PayementBoarding;
use App\Models\Bag;
use App\Models\BoardingPass;
use App\Models\Cruise;
use App\Models\Ticket;
use Illuminate\Http\Request;

class BoardingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $cruise = Cruise::findOrFail($request->id);
        return view('boardings.index', compact('cruise'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $ticket = Ticket::findOrFail($request->ticket_id);
        $weight_bags = $number_bags = 0;
   

        foreach ($request->bags as $bag) {
            $weight_bags += $bag['weight'];
            $number_bags++;
        }

        $request->merge([
            'number_bags'  =>  $number_bags,
            'weight_bags' => $weight_bags
        ]);

        $data = $this->validate($request, [
            'ticket_id' => 'required|numeric',
            'room_id' => 'nullable|numeric',
            'number_bags' => 'nullable|numeric',
            'weight_bags' => 'nullable|numeric',
        ]);

        $data['code'] = time();

        if ($request->weight_bags > $ticket->segment->cruise->weight) {
            $data['amount'] = ($request->weight_bags - $ticket->segment->cruise->weight) * intval(config('app.amount_weight'))*100;
        }


        $boardingPass = BoardingPass::create($data);
        if ($boardingPass->amount > 0) {
            PayementBoarding::dispatch($boardingPass);
        }

        foreach ($request->bags as $bag) {
            Bag::create([
                'boarding_pass_id' => $boardingPass->id,
                'weight' => $bag['weight'],
                'code' => Bag::uniqid('md')
            ]);
        }


        $ticket->ticket_status = Ticket::STATE_‫‪USED‬‬;
        $ticket->save();
        return BoardingPass::with('ticket.car')->where('id',$boardingPass->id)->firstOrFail();
    }

    /**
     * Display the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return BoardingPass::with('ticket.car')->where('ticket_id', $id)->first();
    }


    public function print(BoardingPass $boardingPass)
    {
        return view('boardings.print', compact('boardingPass'));
    }

    public function printCar($boardingPass,$id)
    {
        
        $boardingPass =  BoardingPass::with(['ticket.car' => function($query) use($id){
            $query->where('id', '=', $id);
        }] )->where('id', $boardingPass)->firstOrFail();
        $ticket = $boardingPass->ticket->car;
        return view('boardings.print_car', compact('boardingPass','ticket'));
    }

    public function sticker(Bag $bag)
    {
        $boardingPass = $bag->boardingPass;
        return view('boardings.sticker', compact('boardingPass', 'bag'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\BoardingPass  $boardingPass
     * @return \Illuminate\Http\Response
     */
    public function destroy(BoardingPass $boardingPass)
    {
        return $boardingPass->delete();
    }
}
