<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\BooksPublished;
use Carbon\Carbon;

class BooksPublishedTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $authors = [
            'Dr. A. P. J. Abdul Kalam', 
            'Chetan Bhagat', 
            'Arundhati Roy', 
            'Ravinder Singh', 
            'Kiran Desai',
            'Jhumpa Lahiri', 
            'Ruskin Bond'
        ];
        $titles = [
            'Wings of Fire', 
            'Half Girlfriend', 
            'The God of Small Things', 
            'I Too Had a Love Story', 
            'The Inheritance of Loss',
            'The Namesake',
            'The Blue Umbrella'
        ];
        $publishers = [
            'Penguin Random House', 
            'HarperCollins', 
            'Rupa Publications', 
            'Scholastic India', 
            'Bloomsbury Publishing'
        ];

        // Number of records to seed
        $numberOfRecords = 10;

        for ($i = 1; $i <= $numberOfRecords; $i++) {
            BooksPublished::create([
                'faculty_id' => rand(1, 5),
                'department_id' => rand(1, 5),
                'author' => $authors[array_rand($authors)],
                'title' => $titles[array_rand($titles)],
                'publisher' => $publishers[array_rand($publishers)],
                'issn' => rand(0, 1) ? $this->generateISSN() : null,
                'date_of_publication' => Carbon::now()->subYears(rand(0, 10))->subMonths(rand(0, 12))->subDays(rand(0, 30))->format('Y-m-d'),
                'proof' => null, // No proof uploaded
            ]);
        }
    }

    private function generateISSN()
    {
        $firstPart = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
        $secondPart = str_pad(rand(0, 999), 3, '0', STR_PAD_LEFT);
        $checkDigit = rand(0, 9);
        // Randomly replace the last digit with 'X'
        if (rand(0, 1)) {
            $checkDigit = 'X';
        }
        return "{$firstPart}-{$secondPart}{$checkDigit}";
    }
}
