<?php

namespace Database\Factories;

use App\Models\InvoiceBilling;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\InvoiceBilling>
 */
class InvoiceBillingFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = InvoiceBilling::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $modules = ['gib', 'logo', 'parasut'];
        $module = $this->faker->randomElement($modules);

        $displayNames = [
            'gib' => 'GİB E-Fatura',
            'logo' => 'Logo E-Fatura',
            'parasut' => 'Paraşüt E-Fatura',
        ];

        $defaultSettings = [
            'gib' => [
                'api_url' => 'https://efatura-test.uyumsoft.com.tr/Services/Integration',
                'username' => $this->faker->userName(),
                'password' => $this->faker->password(),
                'test_mode' => true,
            ],
            'logo' => [
                'api_url' => 'https://api.logo.com.tr',
                'api_key' => $this->faker->uuid(),
                'test_mode' => true,
            ],
            'parasut' => [
                'api_url' => 'https://api.parasut.com',
                'client_id' => $this->faker->uuid(),
                'client_secret' => $this->faker->uuid(),
                'test_mode' => true,
            ],
        ];

        $defaultMapping = [
            'gib' => [
                'client_tax_number' => 'client.tax_number',
                'client_name' => 'client.company_name',
                'client_address' => 'client.billing_address',
                'invoice_number' => 'invoice_number',
                'invoice_date' => 'created_at',
                'total_amount' => 'total_amount',
            ],
            'logo' => [
                'client_tax_number' => 'client.tax_number',
                'client_name' => 'client.company_name',
                'client_address' => 'client.billing_address',
                'invoice_number' => 'invoice_number',
                'invoice_date' => 'created_at',
                'total_amount' => 'total_amount',
            ],
            'parasut' => [
                'client_tax_number' => 'client.tax_number',
                'client_name' => 'client.company_name',
                'client_address' => 'client.billing_address',
                'invoice_number' => 'invoice_number',
                'invoice_date' => 'created_at',
                'total_amount' => 'total_amount',
            ],
        ];

        return [
            'name' => $module,
            'display_name' => $displayNames[$module],
            'type' => $this->faker->randomElement(['e-invoice', 'e-archive']),
            'is_active' => $this->faker->boolean(80),
            'is_automatic' => $this->faker->boolean(60),
            'settings' => $defaultSettings[$module],
            'mapping_fields' => $defaultMapping[$module],
            'status' => $this->faker->randomElement(['active', 'inactive', 'error']),
            'last_error' => $this->faker->optional(0.2)->sentence(),
            'last_sync_at' => $this->faker->optional(0.7)->dateTimeBetween('-1 month', 'now'),
        ];
    }

    /**
     * GİB E-Fatura modülü
     */
    public function gib(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'gib',
            'display_name' => 'GİB E-Fatura',
            'type' => 'e-invoice',
            'is_active' => true,
            'is_automatic' => true,
            'status' => 'active',
        ]);
    }

    /**
     * Logo E-Fatura modülü
     */
    public function logo(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'logo',
            'display_name' => 'Logo E-Fatura',
            'type' => 'e-invoice',
            'is_active' => true,
            'is_automatic' => false,
            'status' => 'active',
        ]);
    }

    /**
     * Paraşüt E-Fatura modülü
     */
    public function parasut(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'parasut',
            'display_name' => 'Paraşüt E-Fatura',
            'type' => 'e-invoice',
            'is_active' => true,
            'is_automatic' => false,
            'status' => 'active',
        ]);
    }

    /**
     * Pasif modül
     */
    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => false,
            'status' => 'inactive',
        ]);
    }

    /**
     * Hatalı modül
     */
    public function error(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'error',
            'last_error' => $this->faker->sentence(),
        ]);
    }
}
