<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

use App\Models\CompanySetting;


class CompanySettingsController extends Controller
{
    public function index()
    {
        // Retrieve the company settings (assuming there's only one record)
        $companySettings = CompanySetting::first();

        return response()->json(['companySettings' => $companySettings]);
    }


    public function company_info()
    {
        $company = CompanySetting::first();
        return response()->json(['status'=>true, 'data' => $company]);
    }

    public function store(Request $request)
    {

        // Validate the request data manually
        $validator = Validator::make($request->all(), [
            'Name' => 'required',
            'Address' => 'required',
            'PostCode' => 'required',
            'EmailAddress' => 'required|email',
            'ServerName' => 'required',
            'ShowPrice' => 'required',
            'Sunday' => 'required',
            'Monday' => 'required',
            'Tuesday' => 'required',
            'Wednesday' => 'required',
            'Thursday' => 'required',
            'Friday' => 'required',
            'Saturday' => 'required',
            // Add validation rules for other fields as needed
        ]);

        // Check if validation fails
        if ($validator->fails()) {
            return response()->json([
                'status' => false,
                'message' => 'Validation Error',
                'errors' => $validator->errors(),
            ], 422);
        }

        $hashedServerName = md5($request->input('ServerName'));

        // Create or update the company settings
        $companySettings = CompanySetting::firstOrNew();
        $companySettings->fill($request->all());
        $companySettings->ServerName = $hashedServerName;
        $companySettings->save();


        return response()->json([
            'status' => true,
            'message' => 'Company settings saved successfully',
            'data' => $companySettings
        ], 200);


    }
}