<?php

namespace App\Http\Controllers\Admin;

use App\Customer;
use App\CustomerDocument;
use App\Document;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;

class AdminController extends Controller
{
    public function store(Request $request)
    {
        $name = $request['name'];
        $email = $request['email'];
        $phone = $request['phone'];
        $pass = $request['password'];
        $repass = $request['re-password'];

        if ($pass == $repass) {

            $save_register = new User();

            $save_register->name = $name;
            $save_register->email = $email;
            $save_register->phone = $phone;
            $save_register->password = bcrypt($pass);
            $save_register->role = '1';
            $save_register->status = '1';

            $save_register->save();

            return redirect('/')->withSuccess('Register Successfully');

        } else {

            return redirect()->back()->withErrors('Password Mismatch, Try Again!');

        }
    }


    public function sendFeedbackMail(Request $request)
    {
        $name = $request->name;
        $email = $request->email;
        $description = $request->description;

        $data = array(
            'name' => $request->name,
            'email' => $request->email,
            'description' => $request->description,
        );

        Mail::send('feedback', $data, function ($message) use ($data) {

            $message->from($data['email']);
            $message->to('support@chiseltechnologies.com')
                ->subject('Message From ' . $data['name']);

        });

        return redirect()->back()->withSuccess('Your Message send Successfully');
    }


    public function login(Request $request)
    {
        $user_id = $request['email'];
        $pass = $request['password'];
        $admin_check = $request['admin_check'];

        if ($admin_check == 1) {

            if (
                Auth::attempt(array(
                    'email' => $user_id,
                    'password' => $pass,
                    'status' => '1',
                    'role' => $admin_check
                ))

                ||

                Auth::attempt(array(
                    'name' => $user_id,
                    'password' => $pass,
                    'status' => '1',
                    'role' => $admin_check
                ))

                ||

                Auth::attempt(array(
                    'phone' => $user_id,
                    'password' => $pass,
                    'status' => '1',
                    'role' => $admin_check
                ))
            ) {

                return redirect('/dashboard')
                    ->withSuccess('Welcome to Admin');

            } else {

                return redirect()->back()
                    ->withErrors('Invalid Username Password');

            }

        } else {

            return redirect()->back()
                ->withErrors('Invalid Role');

        }
    }


    public function dashboardView(Request $request)
    {
        /*
        |--------------------------------------------------------------------------
        | Dashboard Statistics
        |--------------------------------------------------------------------------
        */

        // Total uploaded document files
        $totel_documents = Document::count();

        // Total customers
        $totel_customers = Customer::count();

        // Documents created today
        $totel_today_coc = CustomerDocument::whereDate(
            'created_at',
            '=',
            Carbon::today()
        )->count();


        /*
        |--------------------------------------------------------------------------
        | Latest Documents
        |--------------------------------------------------------------------------
        */

        $get_documents = CustomerDocument::orderBy('id', 'DESC')
            ->limit(6)
            ->get();


        /*
        |--------------------------------------------------------------------------
        | Previous Months
        |--------------------------------------------------------------------------
        |
        | Get document records grouped by YEAR-MONTH.
        |
        | Example:
        | 2026-08
        | 2026-07
        | 2026-06
        |
        | Only months which actually contain documents are displayed.
        |
        */

        $get_documents_months = CustomerDocument::select('created_at')
            ->orderBy('created_at', 'DESC')
            ->get()
            ->groupBy(function ($date) {
                return Carbon::parse($date->created_at)->format('Y-m');
            })
            ->take(6);


        /*
        |--------------------------------------------------------------------------
        | Dashboard View
        |--------------------------------------------------------------------------
        */

        return view('dashboard')->with([
            'totel_docs' => $totel_documents,
            'tot_customer' => $totel_customers,
            'tot_today' => $totel_today_coc,
            'get_documents' => $get_documents,
            'get_documents_months' => $get_documents_months
        ]);
    }


    public function dashboardDocView($id)
    {
        $show_documents = CustomerDocument::where('id', $id)->get();

        return view('customers.document_view')
            ->with([
                'documents' => $show_documents
            ]);
    }
}