<?php

namespace App\Helpers\FirstParty\Purchase;

use App\Helpers\FirstParty\Stripe\StripeHelper;
use App\Models\UserUsage;
use Laravel\Cashier\Events\WebhookReceived;

class WatermarkUsageHelper
{
    public static function handleWatermarkUsageWebhookEvents(WebhookReceived $event)
    {
        switch ($event->payload['type']) {
            case 'invoice.paid':
                self::handleInvoicePaid($event);
                break;

            case 'customer.subscription.deleted':
                self::handleClearWatermarkCredits($event);
                break;
        }
    }

    private static function handleClearWatermarkCredits(WebhookReceived $event)
    {
        $object = StripeHelper::getEventObject($event);

        $user = StripeHelper::getUserByStripeID($object['customer']);

        if ($user) {

            foreach ($object['items']['data'] as $line_item) {

                $stripe_price_id = $line_item['plan']['id'];

                $subscription_config = PurchaseHelper::getSubscriptionPlanByStripePriceID($stripe_price_id);

                if ($subscription_config['type'] == 'subscription_plans') {
                    // revert the watermark credits back to 0

                    $user_usage = UserUsage::where('user_id', $user->id)->first();

                    if ($user_usage) {
                        $user_usage->update([
                            'non_watermark_videos_left' => 0,
                        ]);
                    } else {
                        $user_usage = UserUsage::create([
                            'user_id' => $user->id,
                            'non_watermark_videos_left' => 0,
                        ]);
                    }
                }
            }
        }
    }

    private static function handleInvoicePaid(WebhookReceived $event)
    {
        $object = $event->payload['data']['object'];

        // dump($object);

        $accept_statuses = ['paid', 'partially_paid'];

        if (! in_array($object['status'], $accept_statuses)) {
            return;
        }

        $user = StripeHelper::getUserByStripeID($object['customer']);

        if ($user) {
            foreach ($object['lines']['data'] as $line_item) {
                $stripe_price_id = $line_item['pricing']['price_details']['price'];

                // dd($stripe_price_id);

                $subscription_config = PurchaseHelper::getSubscriptionPlanByStripePriceID($stripe_price_id);

                if ($subscription_config) {

                    if ($subscription_config['type'] == 'subscription_plans') {
                        $user_usage = UserUsage::where('user_id', $user->id)->first();

                        if ($user_usage) {
                            $user_usage->update([
                                'non_watermark_videos_left' => $subscription_config['system']['non_watermark_videos'],
                            ]);
                        } else {
                            $user_usage = UserUsage::create([
                                'user_id' => $user->id,
                                'non_watermark_videos_left' => $subscription_config['system']['non_watermark_videos'],
                            ]);
                        }
                    }

                    // dd($subscription_config);
                }
            }
        }
    }
}
