<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use setasign\Fpdi\Fpdi;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class ConclusionController extends Controller
{
    public function create($id, $type)
    {
        // type is enum name: 'AUTO' or 'ESTATE'
        $orderType = $type === 'AUTO' ? 'auto_' : 'estate_';
        $table = $type === 'AUTO' ? 'auto_orders' : 'estate_orders';

        $order = DB::table($table . ' as o')
            ->selectRaw('o.*, o.ordered_customer as customer, CONCAT_WS(\' \', o.owner_last_name, o.owner_first_name, o.owner_patronymic) as owner')
            ->where('o.id', $id)->first();

        if ($order) {
            $dillerUser = $order->diller_id ? DB::table('users')->find($order->diller_id) : null;
            $order->diller = $dillerUser->name ?? '';
        }

        return view('conclusion.add', compact('id', 'type', 'order'));
    }

    public function store(Request $request)
    {
        $orderId   = $request->input('order_id');
        $orderType = $request->input('order_type'); // 'AUTO' or 'ESTATE'
        $dbType    = $orderType === 'AUTO' ? 'auto_' : 'estate_';
        $table     = $orderType === 'AUTO' ? 'auto_orders' : 'estate_orders';

        // Update order prices
        $updateData = ['updated_at' => now()];
        if ($request->filled('object_price')) {
            $updateData['object_price'] = preg_replace('/\D/', '', $request->object_price);
        }
        if ($request->filled('price')) {
            $updateData['price'] = preg_replace('/\D/', '', $request->price);
        }
        DB::table($table)->where('id', $orderId)->update($updateData);

        // Create debit if is_diller = yes
        if ($request->input('is_diller') === 'yes') {
            $order = DB::table($table)->find($orderId);
            DB::table('debits')->insert([
                'order_id'     => $orderId,
                'order_type'   => $dbType,
                'appraiser_id' => auth()->id(),
                'cost'         => $order->cost ?? 0,
                'status'       => 'nopaid',
                'expired'      => $request->input('expired'),
                'customer'     => $order->ordered_customer ?? '',
                'created_at'   => now(),
                'updated_at'   => now(),
            ]);
        }

        // Handle PDF file upload with QR watermark
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $ext  = strtolower($file->getClientOriginalExtension());
            $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
            $size = $file->getSize();

            $dir = 'public/attachments/' . $dbType . $orderId;
            Storage::makeDirectory($dir);

            $tmpPath    = $file->getPathname();
            $outputName = Str::uuid() . '.pdf';
            $outputPath = storage_path('app/' . $dir . '/' . $outputName);

            if ($ext === 'pdf') {
                // Add QR watermark to PDF
                try {
                    $qrPath = storage_path('app/public/attachments/' . $dbType . $orderId . '/qr.png');
                    if (!file_exists($qrPath)) {
                        Storage::makeDirectory('public/attachments/' . $dbType . $orderId);
                        $cleanType = strtolower($orderType);
                        $qrContent = route('qr.verify', ['type' => $cleanType, 'id' => $orderId]);
                        $qrImage   = QrCode::format('png')->size(150)->generate($qrContent);
                        file_put_contents($qrPath, $qrImage);
                    }

                    $pdf       = new Fpdi();
                    $pageCount = $pdf->setSourceFile($tmpPath);

                    for ($i = 1; $i <= $pageCount; $i++) {
                        $tpl = $pdf->importPage($i);
                        $size2 = $pdf->getTemplateSize($tpl);
                        $pdf->AddPage($size2['orientation'], [$size2['width'], $size2['height']]);
                        $pdf->useTemplate($tpl);
                        // Add QR to top-right corner
                        $pdf->Image($qrPath, $size2['width'] - 45, 15, 30, 30);
                    }

                    // Append certificates from the database
                    $certificates = DB::table('certificates')->orderBy('sort')->get();
                    foreach ($certificates as $certificate) {
                        $certPath = storage_path('app/' . $certificate->path);
                        if (file_exists($certPath)) {
                            // Support both PDF and Image certificates
                            $extCert = strtolower(pathinfo($certPath, PATHINFO_EXTENSION));
                            if ($extCert === 'pdf') {
                                $certPageCount = $pdf->setSourceFile($certPath);
                                for ($j = 1; $j <= $certPageCount; $j++) {
                                    $tplCert   = $pdf->importPage($j);
                                    $sizeCert  = $pdf->getTemplateSize($tplCert);
                                    $pdf->AddPage($sizeCert['orientation'], [$sizeCert['width'], $sizeCert['height']]);
                                    $pdf->useTemplate($tplCert);
                                    // Add watermark to certificate page too
                                    $pdf->Image($qrPath, $sizeCert['width'] - 45, 15, 30, 30);
                                }
                            } elseif (in_array($extCert, ['jpg', 'jpeg', 'png'])) {
                                $pdf->AddPage('P', 'A4');
                                $pdf->Image($certPath, 0, 0, 210, 297);
                                // Add watermark
                                $pdf->Image($qrPath, 210 - 45, 15, 30, 30);
                            }
                        }
                    }

                    $pdf->Output('F', $outputPath);
                } catch (\Exception $e) {
                    // If PDF manipulation fails, just copy the original
                    copy($tmpPath, $outputPath);
                }
            } else {
                $file->move(storage_path('app/' . $dir), $outputName);
            }

            $storagePath = '/storage/attachments/' . $dbType . $orderId . '/' . $outputName;

            DB::table('files')->insert([
                'id'            => (string) Str::uuid(),
                'path'          => $storagePath,
                'extension'     => $ext,
                'order_id'      => $orderId,
                'order_type'    => 'conclusion_',
                'type'          => 'conclusion',
                'name'          => $name,
                'size'          => round($size / 1024 / 1024, 2),
                'size_in_bytes' => $size,
                'created_at'    => now(),
                'updated_at'    => now(),
            ]);

            DB::table($table)->where('id', $orderId)->update([
                'status'     => \App\Enums\OrderStatusEnum::FINISHED->name,
                'updated_at' => now(),
            ]);
        }

        $route = $orderType === 'AUTO' ? 'auto.show' : 'estate.show';
        return redirect()->route($route, $orderId);
    }

    public function reject(Request $request)
    {
        $orderId   = $request->input('order_id');
        $orderType = $request->input('order_type'); // 'AUTO' or 'ESTATE'
        $table     = $orderType === 'AUTO' ? 'auto_orders' : 'estate_orders';

        DB::table($table)->where('id', $orderId)->update([
            'status'     => 'rejected',
            'updated_at' => now(),
        ]);

        return redirect()->back();
    }

    public function testWaterMark()
    {
        return view('conclusion.watermark');
    }
}
