<?php

namespace App\Http\Controllers\APIS\CustomerApi;

use App\Http\Controllers\Controller;
use App\Models\Reports;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;



class DownloadsController extends Controller
{
    //
    public function storePdfData(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => ['required'],
            'device_xid' => ['required'],
            'start_date' => ['required'],
            'end_date' => ['required'],
            'report_type' => ['required'],
            'download_status' => ['required'],
        ]);
        if ($validator->fails()) {
            return response()->json([
                'status' => 422,
                'message' => 'Validation failed',
                'errors' => $validator->errors()
            ]);
        }

        $data =  Reports::create([
            'request_time' => Carbon::now(),
            'user_id' => $request->user_id,
            'device_xid' => $request->device_xid,
            'start_date' => $request->start_date,
            'end_date' => $request->end_date,
            'report_type' => $request->report_type,
            'download_status' => $request->download_status,
        ]);

        if ($data) {
            return response()->json([
                'status' => 200,
                'message' => 'Report data saved successfully',
                'data' => $data
            ]);
        }
    }

    public function fetchReport(Request $request)
    {
        $userId = Auth::id();
        $data = Reports::where('user_id', $userId)->whereNull('deleted_at')->get();
        if($data){
            return response()->json([
                'status' => 200,
                'message' => 'Report data fetch successfully',
                'data' => $data
            ]);

        }else{
            return response()->json([
                'status' => 404,
                'message' => 'Report data not found',
                'data' => $data
            ]);

        }

    }

    public function destroyReport()
    {
        $userId = Auth::id();
        $data = Reports::where('user_id', $userId)->whereNull('deleted_at')->delete();
        if ($data) {
            return response()->json([
                'status' => 200,
                'message' => 'Report data deleted successfully',
                'data' => $data
            ]);
        } else {
            return response()->json([
                'status' => 404,
                'message' => 'Report data not found',
                'data' => $data
            ]);
        }
    }
}
