<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Log;

use DateTime;

class MemberArchive extends Component
{
    public $categories = [];

    public function mount()
    {
        $this->categories = [];
        $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->orderBy('name')->get(), 0);
    }

    public function render()
    {
        return view('livewire.member_archive');
    }

    public function getCategories($records, $indentation)
    {
        foreach ($records as $record) {
            $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
            if (count($record->childs))
                $this->getCategories($record->childs, $indentation + 1);
        }
    }
    
    public function restoreMultiple($ids) {
        $success = true;
        foreach ($ids as $id) {
            $success = $this->restore($id, true);
            if (!$success) break;
        }
        if ($success) {
            session()->flash('success', 'Membri ripristinati con successo');
            $this->emit('reload');
        }
    }

    public function restore($id, $bulk = false)
    {
        try {
            $member = \App\Models\Member::findOrFail($id);

            $member->update([
                'is_archived' => false,
                'archived_date' => null,
                'status' => '',
                'enabled' => true
            ]);

            updateMemberData($id);

            if (!$bulk) {
                session()->flash('success', 'Membro ripristinato con successo');
                $this->emit('reload');
            }
        } catch (\Exception $e) {
            session()->flash('error', 'Errore durante il ripristino: ' . $e->getMessage());
            Log::error('Restore member error', [
                'member_id' => $id,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);

            if ($bulk) {
                return false;
            }
        }

        return true;
    }
    
    public function archiveMultiple($ids) {
        $success = true;
        foreach ($ids as $id) {
            $success = $this->archive($id, true);
            if (!$success) break;
        }
        if ($success) {
            session()->flash('success', 'Membri archiviati con successo');
            $this->emit('reload');
        }
    }

    public function archive($id, $bulk = false)
    {
        try {
            $member = \App\Models\Member::findOrFail($id);

            $member->update([
                'is_deleted' => true,
                'deleted_date' => now(),

                'first_name' => 'Nome Archiviato',
                'last_name' => 'Cognome Archiviato',

                'fiscal_code' => null,
                'email' => null,
                'phone' => null,
                'phone2' => null,
                'phone3' => null,
                'address' => null,
                'zip_code' => null,

                'birth_place' => null,
                'birth_date' => null,
                'birth_city_id' => null,
                'birth_province_id' => null,
                'birth_nation_id' => null,

                'nation_id' => null,
                'province_id' => null,
                'city_id' => null,

                'document_type' => null,
                'document_number' => null,
                'document_from' => null,
                'document_expire_date' => null,
                'document_files' => null,

                'father_name' => null,
                'mother_name' => null,
                'father_email' => null,
                'mother_email' => null,
                'father_phone' => null,
                'mother_phone' => null,
                'father_fiscal_code' => null,
                'mother_fiscal_code' => null,
                'father_doc_number' => null,
                'father_doc_type' => null,
                'mother_doc_number' => null,
                'mother_doc_type' => null,
                'father_document_files' => null,
                'mother_document_files' => null,

                'image' => null,

                'gender' => null,
                'status' => 'archived',
                'exclude_from_records' => true,

                'enabled' => false
            ]);

            updateMemberData($id);

            if (!$bulk) {
                session()->flash('success', 'Membro archiviato con successo');
                $this->emit('reload');
            }
        } catch (\Exception $e) {
            session()->flash('error', 'Errore durante l\'archiviazione: ' . $e->getMessage());
            Log::error('Archive member error', [
                'member_id' => $id,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);

            if ($bulk) {
                return false;
            }
        }

        return true;
    }
}
