<?php

namespace App\Services;

use App\Models\ActivityLog;
use App\Models\Package;
use App\Repositories\PackageRepository;

class PackageService
{
    public function __construct(protected PackageRepository $repository) {}

    public function create(array $data): Package
    {
        $package = $this->repository->create($data);

        ActivityLog::create([
            'user_id' => auth()->id(),
            'action' => 'package_created',
            'resource_type' => Package::class,
            'resource_id' => $package->id,
            'description' => 'Package created: '.$package->name,
        ]);

        return $package;
    }

    public function update(Package $package, array $data): bool
    {
        $result = $this->repository->update($package, $data);

        ActivityLog::create([
            'user_id' => auth()->id(),
            'action' => 'package_updated',
            'resource_type' => Package::class,
            'resource_id' => $package->id,
            'description' => 'Package updated: '.$package->name,
        ]);

        return $result;
    }

    public function delete(Package $package): bool
    {
        $result = $this->repository->delete($package);

        ActivityLog::create([
            'user_id' => auth()->id(),
            'action' => 'package_deleted',
            'resource_type' => Package::class,
            'resource_id' => $package->id,
            'description' => 'Package deleted: '.$package->name,
        ]);

        return $result;
    }
}
