@echo off
chcp 65001 >nul
echo ==========================================
echo TAMBAHKAN SEMUA FILE & METHOD YANG KURANG
echo ==========================================
echo.

echo 1. Menambahkan method di JobPackage.php...
powershell -Command "$content = Get-Content app\Models\JobPackage.php -Raw; $newMethods = @'`n`n    /**`n     * Check if package is draft`n     */`n    public function isDraft(): bool`n    {`n        return $this->status === self::STATUS_DRAFT;`n    }`n`n    /**`n     * Mark package as active (draft → active)`n     */`n    public function markAsActive(): void`n    {`n        $this->update([''status'' => self::STATUS_ACTIVE]);`n    }`n'@; $content = $content -replace '(public function isCompleted\(\): bool)', ($newMethods + '`n`n    public function isCompleted(): bool'); $content | Out-File app\Models\JobPackage.php -Encoding UTF8"
echo    Method isDraft() dan markAsActive() ditambahkan
echo.

echo 2. Menambahkan activate() di PackageController.php...
powershell -Command "$content = Get-Content app\Http\Controllers\Api\Package\PackageController.php -Raw; $newMethod = @'`n`n    /**`n     * Activate package (draft → active)`n     */`n    public function activate(JobPackage $package): JsonResponse`n    {`n        $activated = $this->packageService->activate($package);`n`n        return response()->json([`n            ''success'' => true,`n            ''message'' => ''Paket berhasil diaktifkan'',`n            ''data'' => new PackageResource($activated),`n        ]);`n    }`n'@; $content = $content -replace '(public function submit\(JobPackage \$package\): JsonResponse)', ($newMethod + '`n`n    public function submit(JobPackage $package): JsonResponse'); $content | Out-File app\Http\Controllers\Api\Package\PackageController.php -Encoding UTF8"
echo    activate() ditambahkan di Controller
echo.

echo 3. Menambahkan activate() di PackageService.php...
powershell -Command "$content = Get-Content app\Services\Package\PackageService.php -Raw; $newMethod = @'`n`n    /**`n     * Activate package (draft → active)`n     */`n    public function activate(JobPackage $package): JobPackage`n    {`n        return DB::transaction(function () use ($package) {`n            if (!$package->isDraft()) {`n                throw new PackageException(''Hanya paket dengan status draft yang bisa diaktifkan'');`n            }`n            $package->markAsActive();`n            $this->_clearCache($package->id);`n            Log::info(''Package activated'', [''package_id'' => $package->id, ''ppk_id'' => auth()->id(),]);`n            return $package->fresh();`n        });`n    }`n'@; $content = $content -replace '(public function submit\(JobPackage \$package\): JobPackage)', ($newMethod + '`n`n    public function submit(JobPackage $package): JobPackage'); $content | Out-File app\Services\Package\PackageService.php -Encoding UTF8"
echo    activate() ditambahkan di Service
echo.

echo 4. Menambahkan route activate di routes/api.php...
powershell -Command "$content = Get-Content routes\api.php -Raw; $newRoute = '        Route::post(''packages/{package}/activate'', [PackageController::class, ''activate'']);'; $content = $content -replace '(Route::post\(''packages/{package}/submit'', \[PackageController::class, ''submit''\]\);)', ($newRoute + "`n        " + '$1'); $content | Out-File routes\api.php -Encoding UTF8"
echo    Route activate ditambahkan
echo.

echo 5. Membuat ProcessMatchingJob.php...
if not exist app\Jobs\ProcessMatchingJob.php (
    php artisan make:job ProcessMatchingJob
    echo    ProcessMatchingJob.php dibuat
) else (
    echo    ProcessMatchingJob.php sudah ada
)
echo.

echo 6. Membuat Events...
if not exist app\Events\Package mkdir app\Events\Package
if not exist app\Events\Package\PackageCreated.php (
    php artisan make:event Package/PackageCreated
    echo    PackageCreated.php dibuat
) else (
    echo    PackageCreated.php sudah ada
)
if not exist app\Events\Package\PackageSubmitted.php (
    php artisan make:event Package/PackageSubmitted
    echo    PackageSubmitted.php dibuat
) else (
    echo    PackageSubmitted.php sudah ada
)
if not exist app\Events\Package\PackageUpdated.php (
    php artisan make:event Package/PackageUpdated
    echo    PackageUpdated.php dibuat
) else (
    echo    PackageUpdated.php sudah ada
)
echo.

echo 7. Memperbaiki PackageException.php...
powershell -Command "$content = @'<?php`n`nnamespace App\Exceptions;`n`nuse Exception;`n`nclass PackageException extends Exception`n{`n    public function __construct(string $message = '''', int $code = 422, ?Exception $previous = null)`n    {`n        parent::__construct($message, $code, $previous);`n    }`n`n    public function render($request)`n    {`n        if ($request->expectsJson()) {`n            return response()->json([`n                ''success'' => false,`n                ''message'' => $this->getMessage(),`n            ], $this->getCode());`n        }`n        return response()->json([`n            ''success'' => false,`n            ''message'' => $this->getMessage(),`n        ], $this->getCode());`n    }`n}'@; $content | Out-File app\Exceptions\PackageException.php -Encoding UTF8"
echo    PackageException.php diperbaiki
echo.

echo 8. Clear cache...
php artisan optimize:clear
echo    Cache cleared
echo.

echo ==========================================
echo SEMUA FILE & METHOD TELAH DITAMBAHKAN!
echo ==========================================
echo.
echo TESTING DI POSTMAN:
echo 1. Login: POST /api/auth/login
echo 2. Activate: POST /api/ppk/packages/1/activate
echo 3. Submit: POST /api/ppk/packages/1/submit
echo.
pause