<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\RatificationLetter;
use App\Models\Company;

class UpdateCompanyIdInRatificationLettersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Fetch all ratification letters
        $ratificationLetters = RatificationLetter::all();

        foreach ($ratificationLetters as $letter) {
            // Find the company associated with the user_id in the ratification letter
            $company = Company::where('user_id', $letter->user_id)
                ->first();

            // If a company is found, update the ratification letter with the company_id
            if ($company) {
                RatificationLetter::where('id', $letter->id)
                    ->update(['company_id' => $company->id]);
            }
        }
    }
}
