<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Services\ProjectProfitChartService;
use Illuminate\Http\Request;

class ProjectSummaryResource extends ProjectResource
{
    public function toArray(Request $request): array
    {
        $base = parent::toArray($request);
        $invested = (int) ($this->budget ?? 0);
        $profit = (int) ($this->estimated_profit ?? 0);

        return array_merge($base, [
            'identificationCode' => (string) $this->code,
            'images' => $base['images'] ?? [],
            'currentValue' => number_format($invested + $profit),
            'initialValue' => number_format($invested),
            'profitAmount' => number_format($profit),
            'profitPercent' => $invested > 0 ? round(($profit / $invested) * 100, 1) . '%' : '0%',
            'chart_points' => $this->chart_points
                ?? app(ProjectProfitChartService::class)->chartPointsForProject($this->resource),
            'saleStatus' => $this->sale_registration_enabled ? 'none' : 'pending',
            'purchaseStatus' => 'none',
            'contractText' => $this->contract_text,
            'completionPending' => in_array($this->status, ['waiting', 'unsigned'], true),
        ]);
    }
}
