<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\CardMember;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TransaksiMember extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    private function getData(Request $request)
    {
        // return DB::table('mergetransaksistikerdetail'); 

              $allowedPath = ['nama', 'nopol','card'];

        foreach ( $request->query() as $key => $value ) {
            if (!in_array($key, $allowedPath)) {
                return response()->json([
                    'error' => 'Parameter tidak ditemukan',
                    'suggest' => 'Jangan menebak parameter',
                    'invalid_parameter' => $key,
                ], 400);
            }

            if (empty($value)) {
                return response()->json([
                    'error' => 'Nilai parameter tidak dimasukkan',
                    'parameter' => $key,
                ], 400);
            }
        }

        $nama = $request->query('nama');
        $nopol = $request->query('nopol');
        $card = $request->query('card');

        if (!$nama && !$nopol && !$card) {
            return response()->json([
                "application" => [
                    "name" => "Transaksi Stiker",
                    "environment" => env('APP_ENV'),
                    "version" => "v1.0",
                    "author" => "itcn.co.id",
                    "description" => 'make sure your subscription has been renewed :)'
                ]
            ]);
        } 

        $query = DB::table('mergetransaksistikerdetail');

        if ($nama) {
            $query->where('nama', Str::upper($nama));
        } elseif ($nopol) {
            $query->where('nopol', Str::upper($nopol));
        } elseif ($card) {
            $notrans = CardMember::where('no_card', $card);
            // if (!$notrans) {
            //     // return response()->json(['status' => 'failed', 'result' => 'Kartu tidak ditemukan/tidak terdaftar'], 400);
            //     return null;
            // }
            $query->where('no_id', $notrans);
        }

        return $query;

    }

    public function index(Request $request)
    {

        // $allowedPath = ['nama', 'nopol','card'];

        // foreach ( $request->query() as $key => $value ) {
        //     if (!in_array($key, $allowedPath)) {
        //         return response()->json([
        //             'error' => 'Parameter tidak ditemukan',
        //             'suggest' => 'Jangan menebak parameter',
        //             'invalid_parameter' => $key,
        //         ], 400);
        //     }

        //     if (empty($value)) {
        //         return response()->json([
        //             'error' => 'Nilai parameter tidak dimasukkan',
        //             'parameter' => $key,
        //         ], 400);
        //     }
        // }

        // $nama = $request->query('nama');
        // $nopol = $request->query('nopol');
        // $card = $request->query('card');

        // if (!$nama && !$nopol && !$card) {
        //     return response()->json([
        //         "application" => [
        //             "name" => "Transaksi Stiker",
        //             "environment" => env('APP_ENV'),
        //             "version" => "v1.0",
        //             "author" => "itcn.co.id",
        //             "description" => 'make sure your subscription has been renewed :)'
        //         ]
        //     ]);
        // } 

        // // $query = DB::table('mergetransaksistikerdetail')


        // if ($nama) {
        //     // $query->where('nama', Str::upper($nama));
        //     $query = $this->getData()->where('nama', Str::upper($nama));
        // }

        // if ($nopol) {
        //     // $query->where('nopol', Str::upper($nopol));
        //     $query = $this->getData()->where('nopol', Str::upper($nopol));
        // }

        // if ($card) {
        //     // Log::info($card);
        //     // $query->where('nama', Str::upper($nama));
        //     $notrans = CardMember::where('no_card', $card)->first();
        //     Log::info('ketemu : ' .$notrans->notrans);
        //     // $query->where('no_id', $notrans->notrans);
        //     $query = $this->getData()->where('no_id', $notrans->notrans);
        // }

        // $query = $query
        // Log::info('Data : yg di cari : ' . $request);
        $query = $this->getData($request)
        ->whereRaw('? BETWEEN awal AND akhir', [now()])
        ->first();
        // Log::info($query->toSql());
        
        // Log::info('Data : ' . $query);
        if ($query) {            
            $today = Carbon::now();
            $endDate = $query->akhir;
            $dayCount = $today->diffInDays($endDate);
            $monthCount = $today->diffInMonths($endDate);
            $yearCount = $today->diffInYears($endDate);
            Log::info($query->akhir);

            return response()->json([
                'status' => 'success',
                'status_langganan' => 'active',
                'masa_aktif' => $dayCount . ' Hari.',
                'result' => $query,
            ]);
        } else {
            return response()->json([
                'status' => 'failed',
                'result' => 'Tidak ditemukan data',
            ]);
        }


    }

    public function status (Request $request)
    {
        
        // $allowedPath = ['nama', 'nopol','card'];

        // foreach ( $request->query() as $key => $value ) {
        //     if (!in_array($key, $allowedPath)) {
        //         return response()->json([
        //             'error' => 'Parameter tidak ditemukan',
        //             'suggest' => 'Jangan menebak parameter',
        //             'invalid_parameter' => $key,
        //         ], 400);
        //     }

        //     if (empty($value)) {
        //         return response()->json([
        //             'error' => 'Nilai parameter tidak dimasukkan',
        //             'parameter' => $key,
        //         ], 400);
        //     }
        // }

        // $nama = $request->query('nama');
        // $nopol = $request->query('nopol');
        // $card = $request->query('card');

        // if (!$nama && !$nopol && !$card) {
        //     return response()->json([
        //         "application" => [
        //             "name" => "Transaksi Stiker",
        //             "environment" => env('APP_ENV'),
        //             "version" => "v1.0",
        //             "author" => "itcn.co.id",
        //             "description" => 'make sure your subscription has been renewed :)'
        //         ]
        //     ]);
        // } 

        // if ($nama) {
        //     // $query->where('nama', Str::upper($nama));
        //     $query = $this->getData()->where('nama', Str::upper($nama));
        // }

        // if ($nopol) {
        //     // $query->where('nopol', Str::upper($nopol));
        //     $query = $this->getData()->where('nopol', Str::upper($nopol));
        // }

        // if ($card) {
        //     // Log::info($card);
        //     // $query->where('nama', Str::upper($nama));
        //     $notrans = CardMember::where('no_card', $card)->first();
        //     // Log::info('ketemu : ' .$notrans->notrans);
        //     // $query->where('no_id', $notrans->notrans);
        //     $query = $this->getData()->where('no_id', $notrans->notrans);
        // }

        // $data = $query
        $data = $this->getData($request)
            ->orderBy('tanggal', 'desc')    
            ->get();

        Log::info('Data yg ditemukan : ' . $data);

        if ($data->isNotEmpty()) {
            return response()->json([
                'status' => 'success',
                'result' => $data,
            ]);
        } else {
            return response()->json([
                'status' => 'failed',
                'result' => 'Tidak ditemukan data',
            ]);
        }
    }

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


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
