<?php

namespace App\Http\Controllers;

use App\Models\UserCertificate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
use Validator;
use Illuminate\Support\Str;

class UserCertificateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $rules = [
            'fk_iduser' => 'required',
            'name_certificate' => 'required',
            // 'pass_certificate' => 'required',
            'certificate64'    => 'required',  
        ];
        $input     = $request->only('fk_iduser','name_certificate','pass_certificate','certificate64');
        $validator = Validator::make($input, $rules);
        if ($validator->fails()) {
            return response()->json(['success' => false, 'error' => $validator->messages()]);
        } 
        $storagePath  = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
       
        Storage::disk('public')->put('/certificates/'.$request->name_certificate.'.p12',base64_decode($request->certificate64));
        // $cert=$storagePath.'/certificates/'.$request->name_certificate.'.p12';
        // $newroute=$storagePath.'/certificates/'.$request->name_certificate.'.crt';
        // $pass=$request->pass_certificate;
        //   exec('openssl pkcs12 -legacy -in '.$cert.' -out '.$newroute.' -nodes -passin pass:'.$pass.'', $output, $return_var);    
         
           $usercertificate=UserCertificate::create(['fk_iduser'=>$request->fk_iduser,
   'name_certificate'=>$request->name_certificate,
   'certifying_entity'=>'',
   'date_expire_certificate'=>'2025-12-12 12:12:12',
   'CN_nameuser'=> '',
   'C'=>  '',
  'ST'=>  '',
   'L'=>'',
   'organizationIdentifier'=> '',
   'emailAddress'=> '',
   'serialNumber'=> ''
   ]);
 
 return response()->json(['success' => true, 'data' => array("id_usercertif"=>$request->name_certificate)]);
       
        // // // //exec(' openssl pkcs12 -in C:\Users\EQUIPO\Desktop\llave\cuy.p12 -out C:\Users\EQUIPO\Desktop\llave\otro3.crt -nodes -passin pass:FmsWwmSWQxjqkK5Y', $output, $return_var);
       // if($return_var==0){
          //  $pfxContent = file_get_contents($cert);

// if (!openssl_pkcs12_read($pfxContent, $x509certdata,$pass)) {
//   //echo "el certificado no es valido!!";
//   return response()->json(['success' => false, 'error' => 'password incorrecto']);
// } else {

//   $CertPriv   = array();
//   $CertPriv   = openssl_x509_parse(openssl_x509_read($x509certdata['cert']));
//   $PrivateKey = $x509certdata['pkey'];
//   $pub_key = openssl_pkey_get_public($x509certdata['cert']);
//   $keyData = openssl_pkey_get_details($pub_key);

//   $PublicKey  = $keyData['key'];
//   //return response()->json(['success' => true, 'data' => array("CN"=>$CertPriv['subject']['CN'],"dateexpire"=>date('d/m/Y', $CertPriv['validTo_time_t']),"etitycertify"=>$CertPriv['issuer']['OU']['1'])]);
//  $usercertificate=UserCertificate::create(['fk_iduser'=>$request->fk_iduser,
//   'name_certificate'=>$request->name_certificate,
//   'certifying_entity'=>$CertPriv['issuer']['OU']['1'],
//   'date_expire_certificate'=>date('Y-m-d H:i:s', $CertPriv['validTo_time_t']),
//   'CN_nameuser'=>$CertPriv['subject']['CN'],
//   'C'=> $CertPriv['subject']['C'],
//   'ST'=>$CertPriv['subject']['ST'],
//   // 'ST'=>'LIMA',
//   'L'=>$CertPriv['subject']['L'],
//   //'L'=>'L',
//   'organizationIdentifier'=>$CertPriv['subject']['organizationIdentifier'],
//   'emailAddress'=>$CertPriv['subject']['emailAddress'],
//   'serialNumber'=>$CertPriv['subject']['serialNumber']
//   ]);
//  return response()->json(['success' => true, 'data' => array("id_usercertif"=>$usercertificate->id_usercertificates)]);
// }
        // }else{
        //     //print_r($output);
        //     return response()->json(['success' => false, 'error' => "Contraseña incorrecta"]);
        // };   



    }

    /**
     * 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  \App\Models\UserCertificate  $userCertificate
     * @return \Illuminate\Http\Response
     */
    public function show(UserCertificate $userCertificate)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\UserCertificate  $userCertificate
     * @return \Illuminate\Http\Response
     */
    public function edit(UserCertificate $userCertificate)
    {
        //
    }

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

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