<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // \App\Models\User::factory(10)->create();

        \App\Models\User::factory()->create([
            'name'     => 'Martin Folkerts',
            'email'    => 'martin@sobit.nl',
            'password' => '$2y$10$hZZaAaiv1KXmCqq5vZ6PEeRWzvwGbaHKcfqeEMlTn.y22EEPVtofi',
        ]);
        \App\Models\User::factory()->create([
            'name'     => 'Guillaume Marteau',
            'email'    => 'guillaume@sobit.nl',
            'password' => '$2y$10$Y8RSnCdZp1ye/Qp6k6NGzOGcyO5VzxKUilFG6cczzTZ3d3cJp7iYC',
        ]);

        $this->createChembaProject();
        $this->createXinavaneProject();
        $this->createKakiraProject();


    }

    private function createChembaProject()
    {
        $chembaProject = \App\Models\Project::create([
            'name'           => 'Chemba',
            'mail_template'  => 'Mail template for Chemba',
            'mail_subject'   => 'SmartCane update Chemba for {date}',
            'mail_frequency' => 'Weekly',
            'mail_day'       => 'Friday',
            'download_path'  => 'chemba',
            'created_at'     => '2021-01-01 00:00:00',
            'updated_at'     => '2021-01-01 00:00:00',
        ]);

        foreach ($chembaProject->getMergedTiffList() as $mergedTiff) {
            $download = $chembaProject->downloads()->create([
                'name'       => basename($mergedTiff),
                'path'       => $mergedTiff,
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00'
            ]);
            $download->setStatusSuccess();
        }

        $chembaProject->emailRecipients()->createMany([
            [
                'name'       => 'Martin Folkerts',
                'email'      => 'martin@sobit.nl',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ], [
                'name'       => 'Timon Weitkamp',
                'email'      => 'timon@resiliencebv.com',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ],
        ]);

        $chembaProject->mailings()->createMany([
            [
                'subject'    => 'Chemba 2021-01-01',
                'message'    => 'Chemba 2021-01-01',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ], [
                'subject'    => 'Chemba 2021-01-08',
                'message'    => 'Chemba 2021-01-08',
                'created_at' => '2021-01-08 00:00:00',
                'updated_at' => '2021-01-08 00:00:00',
            ],
        ]);


        foreach ($chembaProject->mailings as $mailing) {
            $mailing->setStatusSuccess();

            $mailing->recipients()->createMany([
                [
                    'name'       => 'Martin Folkerts',
                    'email'      => 'martin@sobit.nl',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ], [
                    'name'       => 'Timon Weitkamp',
                    'email'      => 'timon@resiliencebv.com',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ],
            ]);
            $mailing->attachments()->createMany([
                [
                    'name'       => 'Chemba 2021-01-01',
                    'path'       => 'chemba/2021-01-01',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ], [
                    'name'       => 'Chemba 2021-01-08',
                    'path'       => 'chemba/2021-01-08',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ],
            ]);

        }
    }

    private function createXinavaneProject()
    {
        $project = \App\Models\Project::create([
            'name'           => 'Xinavane',
            'mail_template'  => 'Mail template for Xinavane',
            'mail_subject'   => 'SmartCane update Xinavane for {date}',
            'mail_frequency' => 'Weekly',
            'mail_day'       => 'Friday',
            'download_path'  => 'xinavane',
            'created_at'     => '2021-01-01 00:00:00',
            'updated_at'     => '2021-01-01 00:00:00',
        ]);

        $project->emailRecipients()->createMany([
            [
                'name'       => 'Martin Folkerts',
                'email'      => 'martin@sobit.nl',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ], [
                'name'       => 'Timon Weitkamp',
                'email'      => 'timon@resiliencebv.com',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ],
        ]);

        $project->mailings()->createMany([
            [
                'subject'    => 'Xinavane 2021-01-01',
                'message'    => 'Xinavane 2021-01-01',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ], [
                'subject'    => 'Xinavane 2021-01-08',
                'message'    => 'Xinavane 2021-01-08',
                'created_at' => '2021-01-08 00:00:00',
                'updated_at' => '2021-01-08 00:00:00',
            ],
        ]);

        foreach ($project->mailings as $mailing) {
            $mailing->setStatusSuccess();
            $mailing->recipients()->createMany([
                [
                    'name'       => 'Martin Folkerts',
                    'email'      => 'martin@sobit.nl',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ], [
                    'name'       => 'Timon Weitkamp',
                    'email'      => 'timon@resiliencebv.com',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ],
            ]);

            $mailing->attachments()->createMany([
                [
                    'name'       => 'Xinavane 2021-01-01',
                    'path'       => 'xinavane/2021-01-01',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ], [
                    'name'       => 'Xinavane 2021-01-08',
                    'path'       => 'xinavane/2021-01-08',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ],
            ]);
        }
    }

    private function createKakiraProject()
    {
        $project = \App\Models\Project::create([
            'name'           => 'Kakira',
            'mail_template'  => 'Mail template for Kakira',
            'mail_subject'   => 'SmartCane update Kakira for {date}',
            'mail_frequency' => 'Weekly',
            'mail_day'       => 'Friday',
            'download_path'  => 'kakira',
            'created_at'     => '2021-01-01 00:00:00',
            'updated_at'     => '2021-01-01 00:00:00',
        ]);


        $project->emailRecipients()->createMany([
            [
                'name'       => 'Martin Folkerts',
                'email'      => 'martin@sobit.nl',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ], [
                'name'       => 'Timon Weitkamp',
                'email'      => 'timon@smartfarmingtech.com',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ],
        ]);

        $project->mailings()->createMany([
            [
                'subject'    => 'Kakira 2021-01-01',
                'message'    => 'Kakira 2021-01-01',
                'created_at' => '2021-01-01 00:00:00',
                'updated_at' => '2021-01-01 00:00:00',
            ], [
                'subject'    => 'Kakira 2021-01-08',
                'message'    => 'Kakira 2021-01-08',
                'created_at' => '2021-01-08 00:00:00',
                'updated_at' => '2021-01-08 00:00:00',
            ],
        ]);

        foreach ($project->mailings as $mailing) {
            $mailing->recipients()->createMany([
                [
                    'name'       => 'Martin Folkerts',
                    'email'      => 'martin@sobit.nl',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ], [
                    'name'       => 'Timon Weitkamp',
                    'email'      => 'timon@smartfarmingtech.com',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ],
            ]);
            $mailing->attachments()->createMany([
                [
                    'name'       => 'Kakira 2021-01-01',
                    'path'       => 'kakira/2021-01-01',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ], [
                    'name'       => 'Kakira 2021-01-08',
                    'path'       => 'kakira/2021-01-08',
                    'created_at' => $mailing->created_at,
                    'updated_at' => $mailing->updated_at,
                ],
            ]);
        }
    }
}
