<?php

namespace App\Http\Controllers;

use Inertia\Inertia;
use App\Domain\Feeds\Queries\FeedQueryBuilder;
use App\Domain\Feeds\Service\FeedMediaTransform;

class PurchaseController extends Controller
{
    public function index()
    {
        $user = auth()->user();
        $purchases_feeds = new FeedQueryBuilder($user->purchases()->withTrashed());
        $purchases_feeds = $purchases_feeds->withFeedable()->feed->cursorPaginate(FeedQueryBuilder::PAGINATION_COUNT);
        $nextCursor = get_cursor_hash($purchases_feeds);

        $feeds = [];
        foreach ($purchases_feeds as $purchases_feed) {
            $mediaTransform = (new FeedMediaTransform($purchases_feed))->default();
            $date = [
                'price' => $purchases_feed->pivot->amount,
                'purchase_date' => $purchases_feed->pivot->created_at->format('Y-m-d'),
                'preview' => $mediaTransform->getPreview(),
                'id' => $purchases_feed->id,
                'type' => $purchases_feed->type,
            ];
           $feeds[] = $date;
        }

        if(request()->wantsJson()){
            return ['collections' => $feeds, 'next' => $nextCursor];
        }

        return Inertia::render('Settings/SettingsPurchases', [
            'nextCursor' => $nextCursor,
            'feeds' => $feeds,
        ]);

    }

    public function show($id)
    {

        $purchases_feed = new FeedQueryBuilder(auth()->user()->purchases()->withTrashed());
        $purchases_feed = $purchases_feed->selectByIds($id)->withFeedable()->withUser()->feed->firstOrFail();

        $seller = $purchases_feed->user;
        $seller->name = $seller->name;

        $mediaTransformCommon = (new FeedMediaTransform($purchases_feed))->default()->spot();
        $mediaTransformPaid = (new FeedMediaTransform($purchases_feed))->paid()->spot();

        $purchase = [
            'id' => $purchases_feed->id,
            'title' => $purchases_feed->title,
            'body' => $purchases_feed->body,
            'type' => $purchases_feed->type,
            'price' => $purchases_feed->pivot->amount,
            'purchase_date' => $purchases_feed->pivot->created_at->format('Y-m-d'),

            'preview' => $mediaTransformCommon['preview'],
            'common_medias' => $mediaTransformCommon['medias'],
            'paid_medias' => $mediaTransformPaid['medias'],
        ];

        return Inertia::render('Settings/SettingsPurchasesFile', [
            'purchase' => $purchase,
            'seller' => $seller,
        ]);

    }

    public function downloadPurchases($id)
    {

        $purchases_feed = new FeedQueryBuilder(auth()->user()->purchases()->withTrashed());
        $purchases_feed = $purchases_feed->selectByIds($id)->withFeedable()->feed->firstOrFail();
        $mediaTransformPaid = (new FeedMediaTransform($purchases_feed))->getFullPath()->paid()->spot();

        $purchase_date = $purchases_feed->pivot->created_at->format('Y-m-d');

        $zip_file = "purchase-{$purchases_feed->type}-{$purchase_date}.zip";


        $zip = new \ZipArchive();
        $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
        foreach ($mediaTransformPaid['medias'] as $paid_media) {
            $zip->addFile($paid_media['url'], basename($paid_media['url']));
        }

        $zip->close();

        //unlink(public_path($zip_file));
        return response()->download($zip_file);
    }

}
