<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Models\Benefit;
use App\Models\CorporateBenefit;
use App\Models\MemberBenefit;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Illuminate\Support\Facades\Storage;

class BenefitController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request, $corporate_id)
    {
        $benefits = CorporateBenefit::query()
            ->filter($request->all())
            ->where('corporate_id', $corporate_id)
            ->with('benefit', 'plan')
            ->paginate()
            ->appends($request->all());

        return $benefits;
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('internal::create');
    }

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

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('internal::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('internal::edit');
    }

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

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

    public function memberBenefitImport(Request $request, $corporate_id)
    {
        // $request->validate([
        //     'file' => 'required|file|mimes:xls,xlsx,csv,txt',
        // ]); 
        // $file_name = now()->getPreciseTimestamp(3).'-'.$request->file('file')->getClientOriginalName();
        // $file = $request->file('file')->storeAs('temp', $file_name);

        // $reader = ReaderEntityFactory::createReaderFromFile(Storage::path('temp/'.$file_name));
        // $reader->open(Storage::path('temp/'.$file_name));

        // $headers_map_to_table_fields = Benefit::$doc_headers_to_field_map;

        // $imported_benefit_data = 0;
        // $failed_benefit_data = [];
        // foreach ($reader->getSheetIterator() as $sheet) {
        //     $doc_headers_indexes = [];
        //     foreach ($sheet->getRowIterator() as $index => $row) {
        //         if ($index == 1) { // First Row Must be Header
        //             foreach ($row->getCells() as $index => $cell) {
        //                 $doc_headers_indexes[$index] = rtrim($cell->getValue());
        //             }
        //         } else { // Next Row Should be Data
        //             $new_benefit_data = [];
        //             foreach ($row->getCells() as $index => $cell) {
        //                 $new_benefit_data[$headers_map_to_table_fields[$doc_headers_indexes[$index]]] = $cell->getValue();
        //             }

        //             // $imported_plan_data[] = $new_row; // Insert to Array
        //             // Create Directly
        //             $new_benefit_data['corporate_id'] = $corporate_id;
        //             try {
        //                 Benefit::updateOrCreate([
        //                     'corporate_id' => $corporate_id,
        //                     'code' => $new_benefit_data['code']
        //                 ], $new_benefit_data);

        //                 $imported_benefit_data++;
        //             } catch(\Exception $e) {
        //                 $new_benefit_data['error'] = $e->getMessage();
        //                 $failed_benefit_data[] = $new_benefit_data;
        //             }
        //         }
        //     }

        //     break; //only read first sheet
        // }
        // $reader->close();
        // Storage::delete('temp/'.$file_name);
        // // throw(404);

        // return [
        //     'total_successed_row' => $imported_benefit_data,
        //     'total_failed_row' => count($failed_benefit_data),
        //     'failed_row' => $failed_benefit_data
        // ];
    }
}
