<?php

namespace Database\Factories;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\odel=Loan>
 */
class LoanFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        // Generate a random date for the loan, for example in the past year
        $date = Carbon::instance($this->faker->dateTimeBetween('-1 years', 'now'));

        // Ensure that the due date is always after or on the loan date
        $due_date = Carbon::instance($this->faker->dateTimeBetween($date, '+1 years'));

        $attachmentPath = null;

        // Optionally create an attachment
        if ($this->faker->boolean(50)) {  // 50% chance to have an attachment
            $attachmentPath = $this->generateRealAttachment();
        }

        return [
            'borrower_id' => rand(1, 3),
            'type' => $this->faker->randomElement(['MZ', 'HZ']),
            'amount' => $this->faker->randomFloat(2, 500, 10000),
            'date' => $date,
            'due_date' => $due_date,  // Due date between now and one year from now
            'attachment' => $attachmentPath,  // Store attachment path
        ];
    }

    /**
     * Generate a real attachment (image or PDF) and store it in the public disk.
     *
     * @return string|null The file path of the generated attachment, or null if not created.
     */
    private function generateRealAttachment()
    {
        // You can generate either an image or a dummy PDF
        if ($this->faker->boolean(70)) {  // 70% chance to create an image, 30% to create a PDF
            return $this->generateFakeImage();
        } else {
            return $this->generateFakePDF();
        }
    }

    /**
     * Generate a fake image and store it in the "attachments" folder in the public disk.
     *
     * @return string The path of the stored image relative to the public disk.
     */
    private function generateFakeImage()
    {
        // Create a fake image file using Faker and store it in the public disk
        $filePath = $this->faker->image(
            storage_path('app/public/loans'), 640, 480,
            null, false, false, 'Attachment', null, 'png'
        );

        return "loans/{$filePath}";  // Return the path relative to the public disk
    }

    /**
     * Generate a fake PDF and store it in the "attachments" folder in the public disk.
     *
     * @return string The path of the stored PDF relative to the public disk.
     */
    private function generateFakePDF()
    {
        // Create a new FPDF instance
        $pdf = new \FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 16);

        // Add some text
            $pdf->Cell(40, 10, 'Attachment');

        // Generate a unique filename
        $fileName = $this->faker->uuid . '.pdf';

        // Define the file path in the storage
        $filePath = storage_path('app/public/loans/' . $fileName);

        // Save the generated PDF to the storage path
        $pdf->Output('F', $filePath);  // 'F' for saving to a file

        // Return the path relative to the public storage
        return "loans/{$fileName}";
    }
}
