<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\CourseSubscribe;
use App\Models\Event;
use App\Models\EventSubscribe;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Auth;
class SubscriptionController extends Controller
{
    public function storeEventSubscription(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'event_id' => 'required|exists:events,id',
            'children_ids' => 'required|array',
            'children_ids.*' => 'required|exists:children,id',
        ]);
        if (!is_array($validator) && $validator->fails()) {
            return msg(false, $validator->errors()->first(), validation());
        }

        $event = Event::find($request->event_id);
        foreach ($request->children_ids as $key => $value) {
           $subscribe = EventSubscribe::create([
                'child_id' => $value,
                'event_id'=>$request->event_id,
                'parent_id'=>Auth::guard('api')->user()->id,
                'price'=>$event->price,
            ]);
            $total[] = $event->price;
            $SubscribeIds[] = $subscribe['id'];
        }
        $data['total_price'] = array_sum($total);
        $data['subscribe_ids']= $SubscribeIds;

        return msgdata(true, trans('lang.Success_text'),$data, success());

    }
    public function ChangePaymentStatusEventSubscribes(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'required|in:paid,unpaid',
            'subscribe_ids' => 'required|array',
            'subscribe_ids.*' => 'required|exists:event_subscribes,id',
            'pay_type'=>'required|in:master,apple_pay,wallet'
        ]);
        if (!is_array($validator) && $validator->fails()) {
            return msg(false, $validator->errors()->first(), validation());
        }

        EventSubscribe::whereIn('id',$request->subscribe_ids)->update(['type'=>$request->type,'pay_type'=>$request->pay_type]);
        return msgdata(true, trans('lang.Success_text'),(object)[], success());

    }

    public function storeCourseSubscription(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'course_id' => 'required|exists:courses,id',
            'children_ids' => 'required|array',
            'children_ids.*' => 'required|exists:children,id',
        ]);
        if (!is_array($validator) && $validator->fails()) {
            return msg(false, $validator->errors()->first(), validation());
        }

        $Course = Course::find($request->course_id);
        foreach ($request->children_ids as $key => $value) {
            $subscribe = CourseSubscribe::create([
                'child_id' => $value,
                'course_id'=>$request->course_id,
                'parent_id'=>Auth::guard('api')->user()->id,
                'price'=>$Course->price,
            ]);
            $total[] = $Course->price;
            $SubscribeIds[] = $subscribe['id'];
        }
        $data['total_price'] = array_sum($total);
        $data['subscribe_ids']= $SubscribeIds;

        return msgdata(true, trans('lang.Success_text'),$data, success());

    }
    public function ChangePaymentStatusCourseSubscribes(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'required|in:paid,unpaid',
            'subscribe_ids' => 'required|array',
            'subscribe_ids.*' => 'required|exists:course_subscribes,id',
            'pay_type'=>'required|in:master,apple_pay,wallet'
        ]);
        if (!is_array($validator) && $validator->fails()) {
            return msg(false, $validator->errors()->first(), validation());
        }

        CourseSubscribe::whereIn('id',$request->subscribe_ids)->update(['type'=>$request->type,'pay_type'=>$request->pay_type]);
        return msgdata(true, trans('lang.Success_text'),(object)[], success());

    }
}
