<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Price;
use App\Models\PriceInclude;
use Illuminate\Http\Request;

class PricingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pricing = Price::all();
        return view('admin.plan.index', compact('pricing'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.plan.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'title' => ['required'],
            'description' => ['required'],
            'one_time_price' => ['nullable'],
            'one_time_discount' => ['nullable'],
            'monthly_price' => ['nullable'],
            'monthly_discount' => ['nullable'],
            'yearly' => ['nullable'],
            'yearly_discount' => ['nullable']
        ]);

        $price = new Price();
        $price->title = $request->title;
        $price->description = $request->description;

            $price->one_time_price = $request->one_time_price;
            $price->one_time_discount = $request->one_time_discount;

            $price->monthly_price = $request->monthly_price;
            $price->monthly_discount = $request->monthly_discount;

            $price->yearly = $request->yearly;
            $price->yearly_discount = $request->yearly_discount;
        $price->save();


        foreach ($request->input('include', []) as $data) {
            $price->priceincludes()->create([
                'title' => $data
            ]);
        }

        $notification = array(
            'messege' => 'Created Successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Price::findOrFail($id)->load('priceincludes');
        return view('admin.plan.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'title' => ['required'],
            'description' => ['required'],
            'one_time_price' => ['nullable'],
            'one_time_discount' => ['nullable'],
            'monthly_price' => ['nullable'],
            'monthly_discount' => ['nullable'],
            'yearly' => ['nullable'],
            'yearly_discount' => ['nullable']
        ]);

        $price = Price::findOrFail($id);


        $price->title = $request->title;
        $price->description = $request->description;

            $price->one_time_price = $request->one_time_price;
            $price->one_time_discount = $request->one_time_discount;

            $price->monthly_price = $request->monthly_price;
            $price->monthly_discount = $request->monthly_discount;

            $price->yearly = $request->yearly;
            $price->yearly_discount = $request->yearly_discount;
        $price->save();

        $price->priceincludes()->delete();

        foreach ($request->input('include', []) as $data) {
            $price->priceincludes()->create([
                'title' => $data
            ]);
        }

        $notification = array(
            'messege' => 'Updated Successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $price = Price::findOrFail($id);
        $price->delete();

        $notification = array(
            'messege' => 'Deleted Successfully',
            'alert-type' => 'success'
        );

        return back()->with($notification);
    }

    function deleteIncludes($id)
    {
        $data = PriceInclude::find($id);
        $data->delete();
        return back();
    }
}
