<?php

namespace App\Http\Controllers\API\Lesson;

use App\Models\Day;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class DayController extends Controller
{
    public function allDays()
    {
        $days = Day::select('id', 'title', 'heading', 'image')->get();
        return [
            'data' => $days,
            'status' => 200,
            'message' => 'success',
        ];
    }
    public function index()
    {
        $days = Day::all();
        return [
            'data' => $days,
            'status' => 200,
            'message' => 'success',
        ];
    }
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required',
            'heading' => 'required',
            // 'image' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'errors' => $validator->errors(),
                'status' => 401,
            ]);
        }
        $imageUrl = null;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('images', 'public');
            $imageUrl = URL::to('/') . Storage::url($imagePath);
        }

        $day = Day::create([

            'title' => $request->title,
            'heading' => $request->heading,
            'description' => $request->description,
            'breakfast' => $request->breakfast,
            'lunch' => $request->lunch,
            'dinner' => $request->dinner,
            'image' => $imageUrl

        ]);
        return [
            'data' => $day,
            'status' => 200,
            'message' => 'success',
        ];
    }

    public function edit($id)
    {
        $day = Day::find($id);
        return [
            'data' => $day,
            'status' => 200,
            'message' => 'success',
        ];
    }

    public function update(Request $request,$id)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required',
            'heading' => 'required',
            // 'image' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'errors' => $validator->errors(),
                'status' => 401,
            ]);
        }
        $imageUrl = Day::find($id)->image;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('images', 'public');
            $imageUrl = URL::to('/') . Storage::url($imagePath);
        }

        $day = Day::find($id)->update([

            'title' => $request->title,
            'heading' => $request->heading,
            'description' => $request->description,
            'breakfast' => $request->breakfast,
            'lunch' => $request->lunch,
            'dinner' => $request->dinner,
            'image' => $imageUrl

        ]);
        return [
            'data' => $day,
            'status' => 200,
            'message' => 'success',
        ];
    }
    public function destroy($id)
    {
        $day = Day::find($id)->delete();
        return [
            'data' => $day,
            'status' => 200,
            'message' => 'success',
        ];
    }
    public function allUsers(){
        $users = User::where('role','!=', 'admin')->get();
        return [
            'data' => $users,
            'status' => 200,
            'message' => 'success',
        ];

    }
}
