<?php

namespace Database\Factories;

use App\Models\Member;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class MemberFactory extends Factory
{
    protected $model = Member::class;

    public function definition(): array
    {
        return [
            'member_id' => 'AMB-' . date('Y') . '-' . str_pad($this->faker->unique()->numberBetween(1, 999), 3, '0', STR_PAD_LEFT),
            'name' => $this->faker->name(),
            'father_name' => $this->faker->name('male'),
            'mother_name' => $this->faker->name('female'),
            'spouse_name' => $this->faker->optional()->name(),
            'date_of_birth' => $this->faker->dateTimeBetween('-60 years', '-18 years')->format('Y-m-d'),
            'nid' => $this->faker->unique()->numerify('###############'),
            'phone' => $this->faker->unique()->numerify('017########'),
            'email' => $this->faker->unique()->safeEmail(),
            'present_address' => $this->faker->address(),
            'permanent_address' => $this->faker->address(),
            'occupation' => $this->faker->jobTitle(),
            'organization' => $this->faker->company(),
            'designation' => $this->faker->jobTitle(),
            'photo' => 'members/photos/default.png',
            'nominee_name' => $this->faker->name(),
            'nominee_relation' => $this->faker->randomElement(['Wife', 'Son', 'Daughter', 'Father', 'Mother']),
            'nominee_nid' => $this->faker->numerify('###############'),
            'nominee_photo' => 'members/nominees/default.png',
            'nominee_phone' => $this->faker->numerify('017########'),
            'nominee_address' => $this->faker->address(),
            'registration_date' => $this->faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'),
            'joining_date' => $this->faker->optional()->dateTimeBetween('-1 year', 'now')->format('Y-m-d'),
            'member_type' => $this->faker->randomElement(['regular', 'executive', 'general', 'honorary']),
            'committee_position' => $this->faker->optional()->randomElement(['President', 'Secretary', 'Treasurer', 'Member']),
            'monthly_saving' => $this->faker->randomElement([500, 1000, 1500, 2000]),
            'form_fee' => $this->faker->randomElement([100, 200, 500]),
            'form_fee_paid' => $this->faker->boolean(80),
            'total_saving' => $this->faker->numberBetween(1000, 50000),
            'total_loan' => $this->faker->numberBetween(0, 100000),
            'total_profit' => $this->faker->numberBetween(0, 50000),
            'status' => $this->faker->randomElement(['active', 'inactive', 'pending', 'terminated']),
            'approval_status' => $this->faker->randomElement(['pending', 'approved', 'rejected']),
            'blood_group' => $this->faker->randomElement(['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-', null]),
            'gender' => $this->faker->randomElement(['male', 'female', 'other']),
            'marital_status' => $this->faker->randomElement(['single', 'married', 'divorced', 'widowed', null]),
            'education' => $this->faker->randomElement(['SSC', 'HSC', 'Graduate', 'Post Graduate', null]),
            'emergency_contact' => $this->faker->phoneNumber(),
            'reference' => $this->faker->optional()->name(),
            'previous_committee_experience' => $this->faker->optional()->sentence(),
            'created_by' => 1,
            'remarks' => $this->faker->optional()->sentence(),
        ];
    }

    public function active(): Factory
    {
        return $this->state(function (array $attributes) {
            return [
                'status' => 'active',
                'approval_status' => 'approved',
            ];
        });
    }

    public function pending(): Factory
    {
        return $this->state(function (array $attributes) {
            return [
                'status' => 'pending',
                'approval_status' => 'pending',
            ];
        });
    }

    public function withFormFeePaid(): Factory
    {
        return $this->state(function (array $attributes) {
            return [
                'form_fee_paid' => true,
            ];
        });
    }

    public function withPhoto(): Factory
    {
        return $this->state(function (array $attributes) {
            return [
                'photo' => 'members/photos/' . Str::slug($attributes['name']) . '.jpg',
                'nominee_photo' => 'members/nominees/' . Str::slug($attributes['nominee_name']) . '.jpg',
            ];
        });
    }
}