<?php

namespace Wave\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\SiteSetting;
use App\Models\Role;
use App\Models\User;
use App\Models\Permission;
use App\Models\PermissionGroup;
use App\Models\PermissionRole;
use App\Models\UserAccessLog;
use App\Models\ServiceChat;
use App\Models\ServiceChatMessage;
use App\Models\Company;
use App\Models\CompanyDocument;
use App\Models\CompanyMember;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GeneralInformationController extends Controller
{
    public function saveCompanyDetails(Request $request)
    {
        $request->validate([
            'name_english' => 'required',
            'name_chinese' => 'required',
            'registered_office_address_english' => 'required',
            'registered_office_address_chinese' => 'required',
            'br_number' => 'required',
        ]);

        $current_user = $request->user();

        Company::where('id', $current_user->company_id)->update([
            'name_english' => $request->name_english,
            'name_chinese' => $request->name_chinese,
            'registered_office_address_english' => $request->registered_office_address_english,
            'registered_office_address_chinese' => $request->registered_office_address_chinese,
            'br_number' => $request->br_number,
        ]);

        return response()->json([
            'success' => true,
            'message' => 'company successfully updated',
        ]);
    }

    public function updateCompanyMember(Request $request)
    {
        $request->validate([
            'position' => 'required',
            'title' => 'required',
            'name_english' => 'required',
            'name_chinese' => 'required',
            'phone' => 'required',
            'email' => 'required',
            'document_type' => 'required',
            'country' => 'required',
            'address_english' => 'required',
            'address_chinese' => 'required',
            'year_date' => 'required',
            'month_date' => 'required',
            'day_date' => 'required',
            'document_number' => 'required',
            'city' => 'required',
        ]);

        if ($request->member_id == 0) {
            $member = new CompanyMember();
        } else {
            $member = CompanyMember::where('id', $request->member_id)->first();
        }

        $current_user = $request->user();

        $member->company_id = $current_user->company_id;
        $member->position = $request->position;
        $member->title = $request->title;
        $member->name_english = $request->name_english;
        $member->name_chinese = $request->name_chinese;
        $member->phone = $request->phone;
        $member->email = $request->email;
        $member->document_type = $request->document_type;
        $member->document_number = $request->document_number;
        $member->country = $request->country;
        $member->address_english = $request->address_english;
        $member->address_chinese = $request->address_chinese;
        $member->year_date = $request->year_date;
        $member->month_date = $request->month_date;
        $member->day_date = $request->day_date;
        $member->document_number = $request->document_number;
        $member->city = $request->city;
        $member->save();

        if ($request->hasFile('document_file')) {
            $file = $request->file('document_file');
            $file_name = $request->file('document_file')->getClientOriginalName();
            $sanitizedFileName = str_replace(' ', '_', $file_name); // Replac
            $fileName = time() . '_' . $sanitizedFileName;
            // $request->document_file->move(public_path('images'), $file_name);
            $filePath = 'test/' . $fileName;
            \Storage::disk('s3')->put($filePath, file_get_contents($file), 'public');

            $url = env('AWS_ENDPOINT').env('AWS_BUCKET').'/'.$filePath;

            CompanyDocument::create([
                'company_id' => $current_user->company_id,
                'member_id' => $member->id,
                'filename' => $fileName,
                'url' => $url,
            ]);
        }

        return response()->json([
            'success' => true,
            'message' => 'member successfully updated',
            'data' => CompanyMember::with('documents')->where('id', $member->id)->first(),
        ]);

    }

    public function deleteMemberDocument(Request $request)
    {
        $request->validate([
            'id' => 'required',
        ]);

        $document = CompanyDocument::where('id', $request->id)->first();

        if ($document) {
            $document->delete();
        } else {
            return response()->json([
                'success' => false,
                'message' => 'file not found',
            ]);
        }

        return response()->json([
            'success' => true,
            'message' => 'file successfully deleted',
        ]);
    }

    public function getCompanyDetails(Request $request)
    {
        $current_user = $request->user();

        $company = Company::where('id', $current_user->company_id)->first();
        $company_members = CompanyMember::with('documents')->where('company_id', $current_user->company_id)->get();

        return response()->json([
            'success' => true,
            'message' => '',
            'data' => array(
                'company' => $company,
                'company_members' => $company_members,
            ),
        ]);
    }
}
