<?php

namespace App\Services;

use App\Models\PaymentInvoice;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class PaymentInvoiceService
{
    /**
     * Create Invoice
     */
    public function create(array $data): PaymentInvoice
    {
        return DB::transaction(function () use ($data) {

            return PaymentInvoice::create([
                'customer_email' => $data['customer_email'] ?? null,
                'franchisee_id' => $data['franchisee_id'] ?? null,
                'registration_id' => $data['registration_id'] ?? null,
                'transaction_id' => $data['transaction_id'] ?? null,

                'class_and_customer_details' => $data['details'] ?? null,

                'payment_type' => $data['payment_type'], // stripe, viva, revolut
                'payment_method' => $data['payment_method'] ?? null,
                'payment_intent_id' => $data['payment_intent_id'] ?? null,

                'subtotal' => $data['subtotal'] ?? 0.00,
                'tax' => $data['tax'] ?? 0.00,
                'total' => $data['total'] ?? 0.00,
                'currency' => strtolower($data['currency'] ?? 'usd'),

                'status' => $data['status'] ?? PaymentInvoice::STATUS_DRAFT,
                'due_date' => $data['due_date'] ?? null,
                'paid_at' => $data['paid_at'] ?? null,

                'response' => $data['response'] ?? null,
            ]);
        });
    }
}
