<?php

namespace App\Http\Controllers;

use App\Models\Company;
use Illuminate\Http\Request;
use App\Models\CompanyProDocument;
use PDF;
use Illuminate\Support\Carbon;

class DocumentsController extends Controller
{


    public function generateDocumentPDF($uid)
    {
        $company_pro_document = CompanyProDocument::where('uid', $uid)->first();
        if (!$company_pro_document) abort(404);

        $company = Company::where('id', $company_pro_document->company_id)->first();
        if (!$company) abort(404);

        $pro = $company_pro_document->pro;

        // {token} replacements

        // 1. replace "custom" fields (entered by candidate pro)
        $html = $company_pro_document->content_html;
        if($company_pro_document->custom_fields_data) {
            $customFieldsData = json_decode($company_pro_document->custom_fields_data, true);
            foreach ($customFieldsData as $k => $v) {
                $html = str_replace('{' . $k . '}', '<span>' . $v . '</span>', $html);
            }
        }

        // replace database driven fields
        $html = preg_replace_callback(
            '/{([^}]+)}/',
            function ($match) use ($company, $company_pro_document, $pro) {
                $token = $match[1];
                $replacement = '';
                if(strpos($token, '.') !== FALSE) {
                    $token = explode('.', $token);


                    // if prefixed with * - means pre-send fill
                    $waitForCountersignToShow = false;
                    if($token[0][0] === '*') {
                        $token[0] = substr($token[0], 1);
                        $waitForCountersignToShow = true;
                    }

                    $replacement = @${$token[0]}->{$token[1]};

                    // special cases

                    // if the fields are relating to PRO SIGNATURE, then don't display anything until signing
                    if($token[0] === 'company_pro_document') {
                        if (!$company_pro_document->has_pro_signed &&
                            ($token[1] === 'pro_signatures' || $token[1] === 'pro_signed_at')) {
                            $replacement = '';
                        }
                    }

                    // if you want to $waitForCountersignToShow, then do that
                    if($waitForCountersignToShow && !$company_pro_document->has_hrm_pro_counter_signed){
                        $replacement = '';
                    }

                    if(strpos($token[1], "_at") === strlen($token[1]) - 3) {
                        if(!!$replacement) {
                            $replacement = date('m/d/Y', strtotime($replacement));
                        }
                    }

                }
                else {
                    switch ($token) {
                        case 'TODAY':
                            $replacement = date('Y-m-d');
                            break;
                        default:
                            $replacement = '<span style="color:red;">' . $token . '</span>';
                            break;
                    }
                }
                return $replacement;
            },
            $html
        );

         $pdf = PDF::loadView('layouts.document-pdf', compact('company_pro_document', 'html'));
         return $pdf->stream($uid.'.pdf');

//        return view('layouts.document-pdf', compact('company_pro_document', 'html'));
    }
}
