<?php

namespace App\Http\Controllers;

use App\Models\BasicSettings;
use Illuminate\Http\Request;

class BasicSettingsController extends Controller
{
    public function get(Request $request)
    {
        $project_number = $request->input('project_number');
        $project_name = $request->input('project_name');
        $initial_date = $request->input('initial_date');
        $work_duration = $request->input('work_duration');

        $basic_setting = BasicSettings::where('project_number', $project_number)->first();

        if (!$basic_setting) {
            $basic_setting = BasicSettings::create([
                'project_number' => $project_number,
                'project_name' => $project_name,
                'initial_date' => $initial_date,
                'work_duration' => $work_duration
            ]);
        }

        return response()->json($basic_setting);
    }

    //
    public function update(Request $request)
    {
        $project_number = $request->input('project_number');
        $project_name = $request->input('project_name');
        $initial_date = $request->input('initial_date');
        $work_duration = $request->input('work_duration');

        $basicSettings = BasicSettings::where('project_number', $project_number)->first();

        if (!$basicSettings) {
            return response()->json(['message' => 'Project number not found'], 404);
        }

        $basicSettings->project_name = $project_name;
        $basicSettings->initial_date = $initial_date;
        $basicSettings->work_duration = $work_duration;
        $basicSettings->save();

        return response()->json(['message' => 'Data updated successfully']);
    }
}
