<?php

namespace App\Service\User;

use App\Models\Packages;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

class UserPackageService
{
    public function createPackage(array $data)
    {
        try {
            DB::beginTransaction();

            // Handle images (string array expected from request or file uploads handled before this point)
            $data['images'] = $data['images'] ?? [];

            // Create the package
            $package = Packages::create([
                'vendor_id' => $data['vendor_id'],
                'sub_category_id' => $data['sub_category_id'],
                'name' => $data['name'],
                'description' => $data['description'],
                'images' => $data['images'],
                'price' => $data['price'],
                'quantity' => $data['quantity'],
                'status' => $data['status'] ?? 1,
            ]);

            DB::commit();

            return $package;

        } catch (\Exception $e) {
            DB::rollBack();
            Log::error('Package creation failed: ' . $e->getMessage(), [
                'data' => $data,
                'exception' => $e
            ]);

            throw $e;
        }
    }
}
