<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\ChatGroup;
use App\Models\Ticket;
use App\Models\Response;
use App\Models\Message; // Assuming Message is the model for your messages table
use Illuminate\Support\Facades\DB;

class CheckChatGroupStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'chatgroup:check-status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check ChatGroup status and create tickets if status is closed';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // Fetch all closed chat groups
        $closedChatGroups = ChatGroup::where('status', 'closed')->get();

        foreach ($closedChatGroups as $chatGroup) {
            DB::transaction(function () use ($chatGroup) {
                
                $company = get_company('id',$chatGroup->company_id);
                
                $ticket = insertTicket($chatGroup->email, $company->email, $chatGroup->subject, '','chat',$chatGroup->name,$chatGroup->user_id,'done',true );
                   

                // Fetch all messages from the chat group
                $messages = $chatGroup->messages;

                // Insert each message as a response to the ticket
                foreach ($messages as $message) {
                     
                     // Create a new Response instance
                        $response = new Response;
                    
                        $html = $message->message;
                        
                        if($message->type == 'image'){
                            $html = "<img src='{$message->message}' />";
                        }
                        
                        if($message->type == 'file'){
                            $html = "<a target='_blank' href='{$message->message}'>View File</a>";
                        }
                        
                        
                        // Set the properties of the Response
                        $response->message = $html;
                        $response->ticket_id = $ticket->id;
                        $response->user_id = $message->from == 'user'?0:1; // You may want to dynamically set the user_id based on the authenticated user
                        $response->created_at = $message->created_at;
                        $response->updated_at = $message->updated_at;
                        // Save the response to the database
                        $response->save();

                }

                // Optionally update the chat group status to indicate a ticket has been created
                $chatGroup->update(['status' => 'ticket_created']);
            });
        }

        $this->info('Chat groups checked and tickets created for closed statuses.');

        return 0;
    }
}
